1

I want to find the difference between two Multi dimensional array when key value is same. I have tried every thing and so many Stackoverflow solution but I'm not able to do it.

Array 1 (DbData) :-

$ajaxData = array("phase-5[]" =>
    array(
        '1' => "Admin Phase 1 Task 1",
        '2' => "Admin Phase 1 Task 2" 
    ),"phase-6[]" =>
    array(
        '1' => "Admin Phase 2 Task 1", 
        '2' => "Admin Phase 2 Task 2",
        '3' => "Admin Phase 2 Task 3" 
    ),"phase-7[]" =>
    array( 
        '1' => "Admin Phase 3 Task 1", 
        '2' => "Admin Phase 3 Task 2",
        '3' => "Admin Phase 3 Task 3"
    )
);

Array 2 (AjaxData) :-

$dbData = array("phase-5[]" =>
    array(
        '0' => "Admin Phase 1 Task 1", 
        '1' => "Admin Phase 1 Task 2"
    ),"phase-6[]" =>
    array(
        '0' => "Admin Phase 2 Task 1",
        '1' => "Admin Phase 2 Task 2"
    ),"phase-7[]" =>
    array(
        '0' => "Admin Phase 3 Task 1",
        '1' => "Admin Phase 3 Task 2" 
    )
);

PHP code :-

$ajaxDataList = array();
foreach ($ajaxData as $key => $value) {
    print_r($key);
    foreach ($value as $data) {
        $ajaxDataList[] = $data;
    }
}

$dbDataList = array();
foreach ($dbData as $key => $value) {
    print_r($key);
    foreach ($value as $data) {
        $dbDataList[] = $data;
    }
}
var_dump(array_diff($ajaxDataList, $dbDataList));

O/P from code :-

 array (size=2)
  4 => string 'Admin Phase 2 Task 3' (length=20)
  7 => string 'Admin Phase 3 Task 3' (length=20)

I'm able to find the difference between two array but I'm not able to find diffrence with key. I just need to add key here. But I'm not able to do it.

I need a structure like this,

array (size=2)
  'phase-6[]' => string 'Admin Phase 2 Task 3' (length=20)
  'phase-7[]' => string 'Admin Phase 3 Task 3' (length=20)
2
  • Write one foreach loop for Array 1 then write nested loop to trace keys-value pair for Array 2. Try mapping array1 keys with array 2 with the help of nested loop, you can write output to new array once you noticed difference in looping structure Commented Apr 5, 2018 at 11:22
  • i think array_diff_assoc ( ) is helpful for you it compares also key Commented Apr 5, 2018 at 11:22

3 Answers 3

1

Easy solution with small changes

1.You need to add keys with some unique character (I used /) to all values of each array inside your 2 foreach() code

2.After getting difference iterate over this array and explode with a unique character (I used /) and then use first part as key and second part as value and assign them to your final result array

Do like below:-

$ajaxDataList = array();
foreach ($ajaxData as $key => $value) {
    foreach ($value as $data) {
        $ajaxDataList[] = $key.'/'.$data; //add key with each value
    }
}

$dbDataList = array();
foreach ($dbData as $key => $value) {
    foreach ($value as $data) {
        $dbDataList[] = $key.'/'.$data; // add key with each value
    }
}
$semi_final_array = array_diff($ajaxDataList, $dbDataList);

$final_array = [];

foreach($semi_final_array as $arr){ // iterate over difference array
   $exploded = explode('/',$arr); //explode data
   $final_array[$exploded[0]] = $exploded[1]; // use exploded array as key value pair
}

print_r($final_array);

Output:-https://eval.in/984265

Sign up to request clarification or add additional context in comments.

Comments

1

Write a recursive function.

Note: Your sub array keys must be same, in first array, sub array keys start from 1 and but in second array, it start from 0. If you can make it same this function would work

Ref: http://php.net/manual/en/function.array-diff-assoc.php#73972

function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
              if(!isset($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              elseif(!is_array($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              else
              {
                  $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                  if($new_diff != FALSE)
                  {
                        $difference[$key] = $new_diff;
                  }
              }
          }
          elseif(!isset($array2[$key]) || $array2[$key] != $value)
          {
              $difference[$key] = $value;
          }
    }
    return !isset($difference) ? 0 : $difference;
} 

var_dump( array_diff_assoc_recursive(  $ajaxData, $dbData) );

Demo

4 Comments

Thank you for your time. Your solution is also working. But exact Output is achieved by @Alive to Die. I'm upvoting you because your answer is also working .
@WebRence and that means mine does not work? Just asking what is wrong.
@Andreas. I want to get output like this array (size=2) 'phase-6[]' => string 'Admin Phase 2 Task 3' (length=20) 'phase-7[]' => string 'Admin Phase 3 Task 3' (length=20). But in your answer output just return the diffrence which I already get.
@WebRence That a very unclear way to say you need the key. It took me several minutes to figure out what you meant. Updated the code in my answer.
1

You need to loop both arrays and compare against each other.
Otherwise if one subarray exists in one array but not the other the difference won't be noted.

//Loop once first array and compare against the other
Foreach($ajaxData as $key => $arr){
    If(isset($dbData[$key])){
        $ret[] = array_diff($arr, $dbData[$key]);
    }else{
        // If subarray only exists in one array add it to return array
        $ret[] = $arr;
    }
}
// Then the other way around
Foreach($dbData as $key => $arr){
    If(isset($ajaxData[$key])){
        If(!isset($ret[$key])) $ret[$key] = array_diff($arr, $ajaxData[$key]);
    }else{
        // If subarray only exists in one array add it to return array
        $ret[] = $arr;
    }
}

// echo found diff's.
Foreach($ret as $key => $val){
    If(is_array($val)){
        Foreach($val as $v){
            Echo $key ." " . $v . "\n";
        }
    }
}

https://3v4l.org/ZVc00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.