Computing difference by comparing nested arrays - php

I was under the impression that array_diff evaluates the difference between the values in the two arrays. But somehow it doesn't work...I am guessing nested arrays is the problem here. Clearly array_diff_assoc is not the solution as keys are irrelevant in this case.
I don't even want to make it go nested, just see whether the value (in this case, array) inside are identical or not.
$file_details = array(
array(
"uuid" => "45ebdbaa-380b-483b-80a2-73d7c53088e2",
"filename" => "train_failure.mp3",
),
array("uuid" => "97baa061-4208-4aeb-8136-eb76c0932a3d",
"filename" => "train_work1.mp3"
),
array("uuid" => "ksjdfls6-eb76c0932a3d",
"filename" => "train.mp3"
),
);
$items = array(
array(
"uuid" => "45ebdbaa-380b-483b-80a2-73d7c53088e2",
"filename" => "train_failure.mp3",
),
array(
"uuid" => "1233489eb76c0932a3d",
"filename" => "train.mp3"
),
);
print_r(array_diff($file_details,$items));
This returns an empty array...How should I go about fixing this?
My desired output is
array(
"uuid" => "97baa061-4208-4aeb-8136-eb76c0932a3d",
"filename" => "train_work1.mp3"
),
array(
"uuid" => "ksjdfls6-eb76c0932a3d",
"filename" => "train.mp3"
),
UPDATE -: *I know array_diff doesn't work for 1-d array, I'm just surprised there is no direct php function for doing a comparison on multidimensional arrays.*

Your array items are arrays and cannot be compared as strings (array_diff() will treat all values as strings - for example, it will try to stringify objects via calling their __toString() method).
You can use array_udiff() instead:
$rgResult=array_udiff($file_details, $items, function($rgX, $rgY)
{
return $rgX['uuid']<$rgY['uuid']?-1:$rgX['uuid']!=$rgY['uuid'];
});

array_diff() method is supposed to work with 1-d array.

From the array_diff() man page:
Note:
This function only checks one dimension of a n-dimensional array. Of
course you can check deeper dimensions by using
array_diff($array1[0], $array2[0]);

Related

Multidimensional Array Not Echoing As Expected

I am setting this array manually:
$schools = array(
'Indiana University'=>array(
'initials'=>'IU',
'color'=>'red',
'directory'=>'indiana'
)
);
But it won't echo "IU" when I use:
echo $schools[0][0];
It does show correctly when I do:
print_r($schools);
I'm sure I'm messing up something dumb, but I have no idea what and I've been staring at it for hours. This array is actually part of a larger array with multiple universities, but when I trim it down to just this, it doesn't work.
PHP arrays support two types of keys - numerical and strings.
If you just push a value onto an array, it will use numerical keys by default. E.g.
$schools[] = 'Indiana University';
echo $schools[0]; // Indiana University
However, when you use string keys, you access the array values using the string key. E.g.
$schools = array(
'Indiana University' => array(
'initials' => 'IU',
'color' => 'red',
'directory' => 'indiana'
)
);
echo $schools['Indiana University']['initials']; // UI

Odd array_merge_recursive behavior with string keys

I'm trying to use array_merge_recursive to merge two data structures.
<?php
$testSite = array(
'name' => 'test site',
'modules' => array(
'foo' => 'true',
'bar' => 'true'
)
);
$testData = array(
'modules' => array(
'bar' => 'false'
)
);
$testSite = array_merge_recursive($testSite, $testData);
Note that I'm using strings instead of booleans for debug printing purposes
I would expect $testSite to be the exact same after this code has ran, except for the modules.bar property, which I'd expect to see being changed to false. What happens instead, as seen in this live example, is that bar is turned into an array containing it's old value and the value false is appended to that.
The documentation page reads that this is what will happen for numeric keys, but these are all strings keys. Can anyone shed some light on this?
I think you want array_replace_recursive.
array_merge_recursive() vs. array_replace_recursive()

pushing and apending arrays into an associative array in php

How do I push arrays inside the "adjacencies" key value pair that should have an encapsulated array holding the "arrays" (ie. array(("nodeTo" => "$to"),("nodeTo" => "$to"))) without overwriting them and appending them similiar to "+=". also the push into the key "adjacencies" doesnt seem to pick up the value.
$node[] = array(
"adjacencies" => array(), //inside this array should go all the arrays seprated by commas.
"data" => array(
"color" => $color1,
"type" => $type1
);
// this push doesnt seem to detect the adjacencies value and doesnt really push the array inside of the container array. I also tried $node["adjacencies"][]=array("nodeTo" => "$to"); but it didnt work
$node["adjacencies"]=array("nodeTo" => "$to");
}
If you want multiple arrays within 'adjacencies', append them to the end of the array:
$node[0]['adjacencies'][] = array("nodeTo" => "$to");
Granted, you'll need to know which $node index to work with (if there are multiple nodes).
Edit:
After reading the comments, it looks like the OP's desired array structure is this:
$node = array(
'adjacencies' => array(),
'data' => array(
'color' => $color1,
'type' => $type1,
);
);
Thus, to append additional nodes to the adjacencies array, you can do this:
$node['adjacencies'][] = array('nodeTo' => "$to");
By the way you use $node in the second statement I think you meant:
$node = array(
not:
$node[] = array(
// ^^
Then you can push the array by doing:
$node['adjacencies'][] = array('nodeTo' => $to);

Efficient array value replacement

I have an array of values in PHP looking something like this:
$test = array (
array( 'val' => 2, 'color' => 'blue' ),
array( 'val' => 5, 'color' => 'green' ),
);
I want to go through all elements of $test and add 1 to all val indices. Now I realize a foreach loop could work, but I am looking for something a little more efficient. The array will potentially have 10's of thousands of elements and hundreds of sub-elements.
I am wondering if there is some type of way that PHP can go through and modify just that index throughout the entire array, based on the argument that I set.
you can use array_walk and a closure to do it
array_walk($yourArray,function(&$col){$col['val']++});

Can I store an array within a multidimensional array? (PHP)

It is possible to put an array into a multi dim array? I have a list of user settings that I want to return in a JSON array and also have another array stored in that JSON array...what is the best way to do that if it isn't possible?
A multi dimension is already an array inside an array. So there's nothing stopping you from putting another array in there. Sort of like dreams within dreams :P
Just use associative arrays if you want to give your array meaning
array(
'SETTINGS' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
),
'DATA' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
)
)
EDIT
To answer your comment, $output_files[$file_id]['shared_with'] = $shared_info; translates to (your comment had an extra ] which I removed)
$shared_info = array(1, 2, 3);
$file_id = 3;
$output_files = array(
'3' => array(
'shared_with' => array() //this is where $shared_info will get assigned
)
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced.
any array key can have an array value in php, as well as in json.
php:
'key' => array(...)
json:
"key" : [...]
note: php doesn't support multidimensional arrays as in C or C++. it's just an array element containing another array.

Categories