tags:

views:

53

answers:

3

I have two multidimensional arrays.. with same data... what happens is.. values in one array may be changing internally in one multidimensional array.. here is my array...

$previousA = array();
$previousA["t"] = array("twitter","picasa");
$previousA["d"] = array("youtube","gmail");


$freshA = array();
$freshA["t"] = array("twitter","picasa","gmail");
$freshA["d"] = array("youtube");

at any time total values will be four "twitter","picasa","youtube","gmail" which may inter change from $previousA["t"] <=> $previousA["d"] now i want to know which values change from $previousA["t"] to $previousA["d"] comparing with $freshA

+1  A: 

You can make use of array difference function array_diff as:

$from_d_to_t = array_diff($freshA["t"],$previousA["t"]);
$from_t_to_d = array_diff($freshA["d"],$previousA["d"]);

if($from_d_to_t) {
        echo "Elements moved from d to t are ".implode(',',$from_d_to_t)."\n";
}
if($from_t_to_d) {
        echo "Elements moved from t to d are ".implode(',',$from_t_to_d)."\n";
}

Working code

codaddict
+1  A: 

Thats simple all you need to do is loop through your multi-dimension array(which i assume is of order 2) and use the function array_diff to compare the sub arrays. This function gives difference between two array i.e the extra records in array

Web Developer
A: 

If your elements have to be either in t or d then you really only need to keep track of one of t or d.

ex)
$previousT = array('twitter', 'picasa');
$currentT = array('picasa', 'gmail');

then 

$d_to_t = array_diff($currentT, $previousT);  // 'gmail'
$t_to_d = array_diff($previousT, $currentT);  // 'twitter'
brian_d