I have two arrays, both are multi dimensional and i want to compare them. i do the following.

$arraydiff = array_diff ($array1,$array2);

But the size of $arraydiff is 0, but it should not be. The code works fine if its a normal single dimension array, but now fails.

values for $array1
[1],[5],[DC]
[3],[9],[FC]
[2],[10],[MC]

values for $array2
[1],[5],[DC]
[3],[9],[FC]

So i would expect array diff to be
[2],[10],[MC]
but its not.

Help would be much appreciated

    Well...ehm.....ehm.....where is the code?????????????????

      RTFM 🙂

      From PHP Manual:
      Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

        A way to diff multidimension arrays by a given key:

        /*
        *  Multidimension array subtract by a sub key.
        *
        *    $array1 = array(
        *      array(
        *        "name" => "Foo",
        *        "mail" => "foo@moo.net"
        *      ),
        *      array(
        *        "name" => "Bar",
        *        "mail" => "bar@bie.com"
        *      )
        *    );
        *
        *    $array2 = array(
        *      array(
        *        "name" => "Foo",
        *        "mail" => "foo@moo.net"
        *      ),
        *      array(
        *        "name" => "Xpto",
        *        "mail" => "xpto@xp.to"
        *      )
        *    );
        *
        *    $result = array_subtract($array1, $array2, "name");
        *
        *    $result = array(
        *      array(
        *        "name" => "Bar",
        *        "mail" => "bar@bie.com"
        *      )
        *    );
        *
        *  Authors:
        *    Antonio Fernandes <ampf@egp.up.pt>
        *
        */
        function array_subtract($array1,$array2,$compareString) {
            if (!is_array($array1) || !is_array($array2) || !$compareString) {
                return false;
            }
            $arrResult = array();
            foreach ($array1 as $arrInsideArray1) {
                foreach ($array2 as $arrInsideArray2) {
                    $found=false;
                    if ($arrInsideArray1[$compareString]==$arrInsideArray2[$compareString]) {
                        $found=true;
                        break;
                    }
                }
                if (!$found) {
                    array_push($arrResult,$arrInsideArray1);
                }
            }
            return $arrResult;
        } 

          Thanks for that. I now have my code working.
          Cheers

            Matt...mark this thread as RESOLVED!!!!!

              After doing it TWICE i thought that would be enough!

                I think you haven't understood....you have to click on the link below the page of the thread where you can see....this line!!!!

                Mark Thread Resolved (thread starter only)

                  Write a Reply...