Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i would like to ask for your help since I'm having difficulty resolving this matter. I had created a function to facilitate on array diff but it does not suffice to my needs. Thanks and more power!

<?php
   $arraySession = array(
      'sampleA' => array('1', '2', '3'),
      'sampleB' => array('1', '2', '3'),
   );

$arrayPost = array(
    'sampleA' => array('1'),
    'sampleB' => array('1','2'),
);

result should be:

array(
   'sampleA' => array('2', '3')
   'sampleB' => array('3'),
)

my existing function:

    public function array_diff_multidimensional($session, $post) { 
      $result = array();
      foreach($session as $sKey => $sValue){
          foreach($post as $pKey => $pValue) {
              if((string) $sKey == (string) $pKey) {
                  $result[$sKey] = array_diff($sValue, $pValue);
              } else {
                  $result[$sKey] = $sValue;
              }
          }
      }
      return $result;
    }

Any help would be much appreciated! Happy coding!

share|improve this question
add comment

4 Answers

Not my function and not tested by me, but this was one of the first comments at php.net/array_diff (credit goes to thefrox at gmail dot com)

<?php
function multidimensional_array_diff($a1, $a2) {
    $r = array();

    foreach ($a2 as $key => $second) {
        foreach ($a1 as $key => $first) {
          if (isset($a2[$key])) {
              foreach ($first as $first_value) {
                  foreach ($second as $second_value) {
                      if ($first_value == $second_value) {
                          $true = true;
                          break;
                      }
                  }
                  if (!isset($true)) {
                      $r[$key][] = $first_value;
                  }
                  unset($true);
              }
          } else {
              $r[$key] = $first;
          }
        }
    }
    return $r;
}
?>
share|improve this answer
 
thanks dtbarne, im gonna try this. –  Jay Milagroso May 17 '11 at 5:17
 
Looks like it can only handle one array deep. –  alex May 17 '11 at 5:24
 
hi dtbarne, using this function doesnt procure expected results. try: $arraySession = array( 'sampleA' => array('1', '2', '3'), 'sampleB' => array('1', '2', '3'), ); $arrayPost = array( 'sampleA' => array('1','2','3'), 'sampleB' => array('1',), ); –  Jay Milagroso May 17 '11 at 5:28
add comment

This should do it, assuming all keys occur in both arrays:

$diff = array();
foreach ($session as $key => $values) {
    $diff[$key] = array_diff($values, $post[$key]);
}

Or, because I'm bored and array_map is underused:

$diff = array_combine(
    array_keys($session),
    array_map(function ($a, $b) { return array_diff($a, $b); }, $session, $post)
);

(Assumed well ordered arrays though.)

share|improve this answer
 
Please clarify the givens and edge-cases in your question. I'm not even sure if you mean "two-dimensional" or real "multi-dimensional". –  deceze May 17 '11 at 5:37
add comment

You want something more or less like this:

public function array_diff_multidimensional($arr1, $arr2) {
    $answer = array();
    foreach($arr1 as $k1 => $v1) {
        // is the key present in the second array?
        if (!array_key_exists($k1, $arr2)) {
           $answer[$k1] = $v1; 
           continue;
        }

        // PHP makes all arrays into string "Array", so if both items
        // are arrays, recursively test them before the string check
        if (is_array($v1) && is_array($arr2[$k1])) {
            $answer[$k1] = array_diff_multidimensional($v1, $arr2[$k1]);
            continue;
        }

        // do the array_diff string check
        if ((string)$arr1[$k1] === (string)$arr2[$k1]) {
            continue;
        }

        // since both values are not arrays, and they don't match,
        // simply add the $arr1 value to match the behavior of array_diff
        // in the PHP core
        $answer[$k1] = $v1;
    }

    // done!
    return $answer;
}
share|improve this answer
 
thanks Luke, im gonna try this. –  Jay Milagroso May 17 '11 at 5:19
 
i tried using this and wont procure expected results. try: $arraySession = array( 'sampleA' => array('1', '2', '3'), 'sampleB' => array('1', '2', '3'), ); $arrayPost = array( 'sampleA' => array('1','2','3'), 'sampleB' => array('1',), ); –  Jay Milagroso May 17 '11 at 5:29
 
Whups. Since any array typecast to a string is simply "Array" (I forgot about this from my PHP days), all arrays were considered equal. I moved the array check to before the string check, and now it does work (and works with n-dimensional arrays, to boot). –  Luke Sneeringer May 17 '11 at 14:55
add comment
up vote 0 down vote accepted

i already got it. i just utilized the use of foreach. thanks to all who shared their ideas! happy coding!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.