I have a function to convert a .json file to an array:
function jsonToArray($file) {
$json = json_decode(file_get_contents($file), true);
print_r($json); }
This yields an array like this:
Array (
[field1] => value1
[field2] => Array
(
[subfield1] => subvalue1
[subfield2] => subvalue2
[subfield3] => subvalue3
)
)
To interface with existing code, I need these arrays with the fields and values split, like this:
Array (
[0] => Array
(
[0] => field1
[1] => Array
(
[0] => subfield1
[1] => subfield2
[2] => subfield3
)
)
[1] => Array
(
[0] => value1
[1] => Array
(
[0] => subvalue1
[1] => subvalue2
[2] => subvalue3
)
)
)
The code I came up with works if this structure is maintained for all usage but as that can't be guaranteed I need another solution. I'm sure it's something relatively simple, I just can't crack it. Any hints or insight would be much appreciated.
try this code
$arr = array ('field1' => 'value1',
'field2' => array(
'subfield1' => 'subvalue1',
'subfield2' => 'subvalue2',
'subfield3' => 'subvalue3'));
function array_values_recursive($ary) {
$lst = array();
foreach( $ary as $k => $v ) {
if (is_scalar($v)) {
$lst[] = $v;
} elseif (is_array($v)) {
$lst[] = array_values_recursive($v);
}
}
return array_values($lst);
}
function array_keys_recursive($ary) {
$lst = array();
foreach( $ary as $k => $v ) {
if (is_scalar($v)) {
$lst[] = ($k);
} elseif (is_array($v)) {
$lst[] = array_keys_recursive($v);
}
}
return $lst;
}
echo '<pre>';
$arr1 = array();
$arr1[] = array_values_recursive($arr);
$arr1[] = array_keys_recursive($arr);
print_r($arr1);
This might be useful to you: array_values() and array_keys() that and a little of foreach would do the magic.
Related
Following simplified multidimensional array given:
$input = Array
(
[arr1] => Array
(
[0] => JAN2016
[1] => MAI2013
[2] => JUN2014
}
[arr2] => Array
(
[0] => APR2016
[1] => DEC2013
[2] => JUN2014
}
[arr3] => Array
(
[0] => JAN2016
[1] => MAI2020
[2] => JUN2022
}
)
I want to check for elements, that exists in more than 1 subarray. An ideal output would be:
$output = Array
(
[JUN2014] => Array
(
[0] => arr1
[1] => arr2
)
[JAN2016] => Array
(
[0] => arr1
[1] => arr3
)
)
I'm currently stucked in a nested foreach because i need to look all silblings of the outer foreach and don't know how to accomplish that.
foreach($input as $k=>$values)
{
foreach($values as $value)
{
//check if value exists in array k+1....n
//if true, safe to output.
}
}
You are almost all the way there
$new = [];
foreach($input as $k=>$values) {
foreach($values as $value) {
$new[$value][] = $k;
}
}
The $new array should look just as you want it
Extended solution which filters subarrays:
$newArray = [];
foreach($input as $k=>$values)
{
foreach($values as $value)
{
$newArray[$value][] = $k;
}
}
print_r(array_filter(
$newArray,
function($v) { return 1 < count($v); }
));
Sample fiddle here.
I'm trying to change my array from this:
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
in to this:
Array
(
[BID_OPEN]
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE]
(
[0] => 1.654545
[1] => 1.645845
)
)
I'm not sure how to begin. My code:
foreach($array as $keys=>$values)
{
if(!empty($array [$c]['BID_OPEN']))
{
$inital_part1 = array("BID_OPEN", $array [$c]['BID_OPEN']);
}
else
{
echo '';
}
if(!empty($array [$c]['BID_CLOSE']))
{
$inital_part2 = array("BID_CLOSE", $array [$c]['BID_CLOSE']);
}
else
{
echo '';
}
$array1[] = $inital_part1;
$array1[] = $inital_part2;
$c++;
}
I seem to get double outputs, so the foreach when I build arrays is giving me two times the required output. Google reckons it's because I have an array in my array somewhere but I'm precisely sure I don't.
The array came from an object stdclass and I don't know what that is, have googled but haven't found anything useful. Also I'm able to get some figures but only the initial values are correct, the rest of the data doesn't seem to come through. No doubt it's because I used an index[0] to get it working.
After hours any help would be great thanks.
As long as you have told us everything about your input array it can be done quite simply like this
<?php
$in = [ ['BID_OPEN' => [0.718282]],
['BID_CLOSE' => [1.654545]],
['BID_OPEN' => [1.654878]]
];
print_r($in);
$new = []; // new array we are building
foreach ($in as $abid) {
if (array_key_exists('BID_OPEN', $abid) ) {
$new['BID_OPEN'][] = $abid['BID_OPEN'][0];
}
if (array_key_exists('BID_CLOSE', $abid) ) {
$new['BID_CLOSE'][] = $abid['BID_CLOSE'][0];
}
}
print_r($new);
THE INPUT ARRAY: Is like yours
Array
(
[0] => Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
)
)
[1] => Array
(
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
[2] => Array
(
[BID_OPEN] => Array
(
[0] => 1.654878
)
)
)
RESULT:
Array
(
[BID_OPEN] => Array
(
[0] => 0.718282
[1] => 1.654878
)
[BID_CLOSE] => Array
(
[0] => 1.654545
)
)
$c = 0;
$array1['BID_OPEN'] = [];
$array2['BID_CLOSE'] = [];
foreach($vartttttt as $tunips=>$ert)
{
$d = 0;
foreach($ert as $erts=>$val)
{
//$array[] = $erts;
if($erts == 'BID_OPEN')
{
array_push($array1['BID_OPEN'], $val[0]);
}
if($erts == 'BID_CLOSE')
{
array_push($array2['BID_CLOSE'], $val[0]);
}
$d++;
}
$c++;
}
$array = array_merge($array1, $array2);
I have the following array:
Array
(
[documents] => Array
(
[0] => application/pdf
[1] => application/x-pdf
)
[images] => Array
(
[0] => image/cgm
[1] => image/g3fax
)
[videos] => Array
(
[0] => video/dl
[1] => video/fli
[2] => video/gl
[3] => video/mpeg
)
And I have a couple of tables called documents, images, videos. So what I would like to do is to see in which database the file should go.
I tried to do it with array_search() but without success. After that I found a function which I tried also no luck.
function array_search_multi( $value, array $array ) {
foreach( $array as $key => $val ) {
if( is_array( $val ) ) {
array_search_multi($value, $val); // Recursive in case array is deeper
} else {
if( $val === $value ) {
return $key;
}
}
}
return false;
}
I hope someone could help me with this
If I understand you're looking for something like this
function find($mimeType) {
$array = [
'documents' => ['application/pdf','application/x-pdf'],
'images' => ['image/cgm','image/g3fax'],
'videos' => ['video/dl','video/fli','video/gl','video/mpeg'],
];
$table = null;
foreach ($array as $type => $values) {
$table = $type;
if ( in_array ($mimeType, $values) ) break;
}
return $table;
}
$sample = 'image/g3fax';
echo find($sample);
Here is the raw data
Array
(
[name] => me
[tickets] => Array
(
[1] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 10
)
[2] => Array
(
[name] => DCT
[received] => 3
)
)
)
[2] => Array
(
[equipment] => Array
(
[1] => Array
(
[name] => DVR
[received] => 4
)
[2] => Array
(
[name] => DCT
[received] => 6
)
)
)
)
)
Users have multiple tickets, but each ticket has the same item with different 'received' amounts. I would like to sum the received amount into one variable/array.
Here is a demo of how I would like to get it to work like
Array
(
[name] => me
[equipment] => Array
(
[DVR] => 14
[DCT] => 9
)
)
Here is my most recent failed attempt at building my own array from a multidimensional array.
foreach($data as $user){
$sum = [];
$sum['name'] = $user->name;
$sum['equipment'] = [];
foreach($user->tickets as $ticket){
foreach($ticket->equipments as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['pivot']['received'];
}
}
print_r($sum);
}
Please try the following code. There's only a single user in your $data, though, so you need to do $data = [$data]; first.
foreach ($data as $user) {
$sum = [];
$sum['name'] = $user['name'];
$sum['equipment'] = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipment'] as $eqpt){
$sum['equipment'][$eqpt['name']] += $eqpt['received'];
}
}
print_r($sum);
}
PHP arrays are accessed with square bracket syntax
There's probably a typo in $ticket->equipments.
Try with this:
$array = $data; //$data is your array
$sum = array('name' => $array['name'], 'equipment' => array());
foreach($array['tickets'] as $row) {
for($i = 0; $i < count($row); $i++) {
foreach($row['equipment'] as $infos) {
$sum['equipment'][$infos['name']] += $infos['received'];
//print_r($infos);
}
}
}
print_r($sum);
well, after much googling and trial and error this appears to work
$sum = [];
// $data is a collection returned by Laravel
// I am converting it to an array
foreach($data->toArray() as $user){
$items = [];
foreach($user['tickets'] as $ticket){
foreach($ticket['equipments'] as $eqpt){
$name = $eqpt['name'];
if (! isset($items[$name]))
{
$items[$name] = $eqpt['received'];
} else {
$items[$name] += $eqpt['received'];
}
}
}
$sum[] = [
'name' => $user['name'],
'equipment' => $items
];
}
#tsnorri #Adrian Cid Almaguer
I have a multi-dimensional array like this:
Array
(
[0] => Array
(
[id] => 1
[email_id] => ok#gmail.com
[password] => test
)
[1] => Array
(
[id] => 2
[email_id] => check#gmail.com
[password] => test
)
[2] => Array
(
[id] => 3
[email_id] => an#gmail.com
[password] => pass
)
)
In the above array, password value is the same in two different rows. I need to merge the arrays which have duplicate values to get the following output:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[email_id] => ok#gmail.com
[password] => test
)
[1] => Array
(
[id] => 2
[email_id] => check#gmail.com
[password] => test
)
)
[1] => Array
(
[id] => 3
[email_id] => an#gmail.com
[password] => pass
)
)
How to do this? I've tried array_merge() & foreach() loops, but I can't get this output.
Try,
$arr = array( array('id'=>1, 'email_id'=>'ok#gmail.com', 'password'=>'test'),
array('id'=>2, 'email_id'=>'check#gmail.com', 'password'=>'test'),
array('id'=>3, 'email_id'=>'an#gmail.com', 'password'=>'pass'));
$new_arr = array();
foreach($arr as $k => $v) {
if( is_array($arr[$k+1]) && $arr[$k]['password'] === $arr[$k + 1]['password'] )
$new_arr[] = array($arr[$k], $arr[$k+1]);
else if( in_array_recursive($arr[$k]['password'], $new_arr) === FALSE )
$new_arr[] = $v;
}
function in_array_recursive( $val, $arr) {
foreach( $arr as $v ) {
foreach($v as $m) {
if( in_array($val, $m ) )
return TRUE;
}
}
return FALSE;
}
print_r($new_arr);
Demo
You only want to create more depth in your output array if there is more than one entry in the group. I personally wouldn't want that kind of variability in a data structure because the code that will print the data will need to go to extra trouble to handle associative rows of data that might be on different levels.
Anyhow, here's how I would do it with one loop...
Foreach Loop Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['password']])) {
$result[$row['password']] = $row; // save shallow
} else {
if (isset($result[$row['password']]['id'])) { // if not deep
$result[$row['password']] = [$result[$row['password']]]; // make original deeper
}
$result[$row['password']][] = $row; // save deep
}
}
var_export(array_values($result)); // print without temporary keys
Functional Code: (Demo)
var_export(
array_values(
array_reduce(
$array,
function($result, $row) {
if (!isset($result[$row['password']])) {
$result[$row['password']] = $row;
} else {
if (isset($result[$row['password']]['id'])) {
$result[$row['password']] = [$result[$row['password']]];
}
$result[$row['password']][] = $row;
}
return $result;
},
[]
)
)
);