1

I have 2 arrays:

$big_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer1'],
        ['correct' => FALSE, 'answer' => 'false answer2'],
        ['correct' => FALSE, 'answer' => 'false answer3'],
        ['correct' => TRUE, 'answer' => 'correct answer'],
    ]
];

$small_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer1'],
    ]
];

I want to make array_diff($big_array, $small_array) but it's not working how I want it to.

Currently, it gives me an empty array as a result, where I want the outcome to be:

$outcome_array = [
    [
        ['correct' => FALSE, 'answer' => 'false answer2'],
        ['correct' => FALSE, 'answer' => 'false answer3'],
        ['correct' => TRUE, 'answer' => 'correct answer'],
    ]
];
2
  • first suggestion : post the code that is not working... how else would we be able to tell you what's wrong ? Commented May 3, 2013 at 12:50
  • @bartdude , like i wrote in OP - array_diff($big_array, $small_array); @kingkero , the $big_array has a lot of arrays, i just showed 1 here because as an example, but it has more than 1 Commented May 3, 2013 at 12:52

4 Answers 4

14

yes you can use below function to get you perfect answer

function arrayRecursiveDiff($aArray1, $aArray2) {
  $aReturn = array();

  foreach ($aArray1 as $mKey => $mValue) {
    if (array_key_exists($mKey, $aArray2)) {
      if (is_array($mValue)) {
        $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
        if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
      } else {
        if ($mValue != $aArray2[$mKey]) {
          $aReturn[$mKey] = $mValue;
        }
      }
    } else {
      $aReturn[$mKey] = $mValue;
    }
  }
  return $aReturn;
} 

  $arr1 =    arrayRecursiveDiff($big_array,$small_array);

output

as you need

Array

(
    [0] => Array
        (
            [1] => Array
                (
                    [correct] => 
                    [answer] => false answer2
                )

            [2] => Array
                (
                    [correct] => 
                    [answer] => false answer3
                )

            [3] => Array
                (
                    [correct] => 1
                    [answer] => correct answer
                )

        )

)

check here for more info

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

3 Comments

array_udiff() would be a better choice.. Just sayin..
when making the difference of more than 5000 values,The returned array has 2% of its values that miss the 3rd and 4th associative key of the sub-array
If you are going to copy code from elsewhere, please attribute it. This is an exact copy of code posted as a comment on the PHP array_diff manual page.
0

Edited above code check whether or the the arr1 is bigger or arr2 is bigger

function arrayRecursiveDiff($aArray1, $aArray2) {

if(sizeof($aArray1) > sizeof($aArray2))
{
    $small_arr = $aArray2;
    $big_arr = $aArray1;
    $arr1_is_big = 1;
}
else
{
    $small_arr= $aArray1;
    $big_arr = $aArray2;
    $arr1_is_big = 0;
}
$aReturn = array();

foreach ($big_arr as $mKey => $mValue) {
    if (array_key_exists($mKey, $small_arr)) {
        if (is_array($mValue)) {
            $aRecursiveDiff = arrayRecursiveDiff($mValue, $small_arr[$mKey]);
            if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
            }
        else {
            if ($mValue != $small_arr[$mKey]) {
                $aReturn[$mKey] = $mValue;
                }
            }
    } else {
        $aReturn[$mKey] = $mValue;
    }
}
$aReturn["arr1_is_big"] = $arr1_is_big;

return ($aReturn);
} 

Comments

0

I just edited the function from Murali Kumar cause I needed one more information. It will check if the arrays are the same size and contains the same values.

function arrayRecursiveDiff($aArray1, $aArray2) {
    if(count($aArray1) > count($aArray2)) {
        $small_arr = $aArray2;
        $big_arr = $aArray1;
        $arr1_is_big = 1;
        $arr2_is_big = 0;
    } elseif(count($aArray1) < count($aArray2)) {
        $small_arr = $aArray1;
        $big_arr = $aArray2;
        $arr1_is_big = 0;
        $arr2_is_big = 1;
    } else {
        $small_arr = $aArray1;
        $big_arr = $aArray2;
        $arr1_is_big = 0;
        $arr2_is_big = 0;
    }
    $aReturn = array();

    foreach($big_arr as $mKey => $mValue) {
        if(array_key_exists($mKey, $small_arr)) {
            if(is_array($mValue)) {
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $small_arr[$mKey]);
                if(count($aRecursiveDiff)) {
                    $aReturn[$mKey] = $aRecursiveDiff;
                }
            } else {
                if($mValue != $small_arr[$mKey]) {
                    $aReturn[$mKey] = $mValue;
                }
            }
        } else {
            $aReturn[$mKey] = $mValue;
        }
    }
    if($arr1_is_big)
        $aReturn["arr1_is_big"] = $arr1_is_big;

    if($arr2_is_big)
        $aReturn["arr2_is_big"] = $arr2_is_big;

    return ($aReturn);
}

Comments

0

Your input arrays appear to have correlated data sets. To synchronous iterate each data set and filter out the differences, make array_udiff() calls inside array_map(). Demo

var_export(
    array_map(
        fn($bigSet, $smallSet) => array_udiff(
            $bigSet,
            $smallSet,
            fn($a, $b) => $a <=> $b
        ),
        $big_array,
        $small_array
    )
);

Output:

array (
  0 => 
  array (
    1 => 
    array (
      'correct' => false,
      'answer' => 'false answer2',
    ),
    2 => 
    array (
      'correct' => false,
      'answer' => 'false answer3',
    ),
    3 => 
    array (
      'correct' => true,
      'answer' => 'correct answer',
    ),
  ),
)

If you want indexed rows in the result array, just call array_values() on each filtered row.

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.