Click to See Complete Forum and Search --> : Need syntax help with array_diff


santa
04-29-2008, 09:20 AM
I need help with making this work.

I have two arrays. I need to compare the two and create third array that has values in the 1st array but does not have the values in the second.

Sounds pretty easy and array_diff comes to mind, but I'm getting confused because my second array is an array withing array sort of. Need help with debugging.


$firstArray = array('a', 'b', 'c', 'd', 'e', 'f');

$secondArray = // my values

print_r($secondArray);

//WILL RETURN
//Array ( [0] => Array ( [name] => a [size] => 12345) [1] => Array ( [name] => c [size] => 15447) [2] => Array ( [name] => e [size] => 15447))

I need to be able to do the following:
$thirdArray = array_diff($firstArray, $secondArray);

Stuck... Need help. Thanks.

andre4s_y
04-29-2008, 10:45 AM
I do not understand...
What is the content of the $thirdArray in your example above?

santa
04-29-2008, 11:08 AM
It needs to be a difference of the first two.

andre4s_y
04-29-2008, 12:08 PM
Maybe something like this :

foreach ($secondArray as $key => $value)
{//create temporary array to satisfy the function..
$temp[$key]=$value['name'];
}
$thirdArray = array_diff($firstArray, $temp);
print_r($thirdArray);

It will output :

Array
(
[1] => b
[3] => d
[5] => f
)