force
September 25, 2013, 4:54pm
1
I’m trying to use array_intersect to compare two arrays of arrays.
$start[]=array(
'id'=>1,
'name'=>'Up',
'action'=>'up'
);
$start[]=array(
'id'=>3,
'name'=>'Down',
'action'=>'down'
);
$start[]=array(
'id'=>5,
'name'=>'Left',
'action'=>'left'
);
$end[]=array(
'id'=>1,
'name'=>'Up',
'action'=>'up'
);
$end[]=array(
'id'=>9,
'name'=>'Up',
'action'=>'up'
);
$result=array_intersect($start,$end);
However, I always get the notice message: Notice: Array to string conversion in testfile.php on line xyz
And the comparison doesn’t actually occur.
What is the best way to compare the two arrays without reinventing the wheel or arriving at something overly complex?
cpradio
September 25, 2013, 6:43pm
2
This isn’t mine (I took it from the PHP manual page )
Note that array_intersect and array_unique doesnt work well with multidimensional arrays.
If you have, for example,
<?php
$orders_today[0] = array('John Doe', 'PHP Book');
$orders_today[1] = array('Jack Smith', 'Coke');
$orders_yesterday[0] = array('Miranda Jones', 'Digital Watch');
$orders_yesterday[1] = array('John Doe', 'PHP Book');
$orders_yesterday[2] = array('Z� da Silva', 'BMW Car');
?>
and wants to know if the same person bought the same thing today and yesterday and use array_intersect($orders_today, $orders_yesterday) you’ll get as result:
<?php
Array
(
[0] => Array
(
[0] => John Doe
[1] => PHP Book
)
[1] => Array
(
[0] => Jack Smith
[1] => Coke
)
)
?>
but we can get around that by serializing the inner arrays:
<?php
$orders_today[0] = serialize(array('John Doe', 'PHP Book'));
$orders_today[1] = serialize(array('Jack Smith', 'Coke'));
$orders_yesterday[0] = serialize(array('Miranda Jones', 'Digital Watch'));
$orders_yesterday[1] = serialize(array('John Doe', 'PHP Book'));
$orders_yesterday[2] = serialize(array('Z� da Silva', 'Uncle Tungsten'));
?>
so that array_map(“unserialize”, array_intersect($orders_today, $orders_yesterday)) will return:
<?php
Array
(
[0] => Array
(
[0] => John Doe
[1] => PHP Book
)
)
?>
showing us who bought the same thing today and yesterday =)
force
September 25, 2013, 7:06pm
3
I’ve been trying to parse the arrays every which way with nesting and recursion and failing miserably in an attempt to basically rewrite the array_intersect function.
Serializing didn’t even occur to me, although you have to make sure all the values are in the same order.
So, it looks like I managed to get this working:
<?php
echo '<pre>';
$start[]=array(
'id'=>1,
'name'=>'Up',
'action'=>'up'
);
$start[]=array(
'id'=>3,
'name'=>'Down',
'action'=>'down'
);
$start[]=array(
'id'=>5,
'name'=>'Left',
'action'=>'left'
);
$start[]=array(
'id'=>2,
'name'=>'Left',
'action'=>'left'
);
$end[]=array(
'name'=>'Up',
'id'=>1,
'action'=>'up'
);
$end[]=array(
'id'=>8,
'name'=>'Right',
'action'=>'Right'
);
function serialize_array_values($arr){
foreach($arr as $key=>$val){
sort($val);
$arr[$key]=serialize($val);
}
return $arr;
}
$result = array_map("unserialize", array_intersect(serialize_array_values($start),serialize_array_values($end)));
echo "\
\
\
";
echo var_dump($result);
echo '</pre>';
?>
Ugh, what a waste of half a day for something I would’ve expected to already be built into PHP.
Thanks