Pankaj Pawar
Pankaj Pawar

Reputation: 345

I want to compare two multidimational array or json String

I have two json string, i have convert that string into array when i am sending request via curl.

now my problem is i want to send another request if my string is updated.

i have used array_diff but it's giving me error "Notice: Array to string conversion" because its multidimensional.

my json strings are:

{"toy":["cycle","doll"],"accessory":["tv"]}

and second one is

{"toy":["cycle","cat","rabit"],"accessory":["tv","spekers"]}

how do i differentiate beetween this and get value if its not in another array.

my converted two array is like this

Array

( ['toy'] => Array ( [0] => cycle, [1] => doll ), ['accessory'] => Array ( [0] => tv )

); Array ( ['toy'] => Array ( [0] => cycle, [1] => cat, [2] => rabit ), [accessory] => Array ( [0] => tv, [1] => spekers )

);

Upvotes: 0

Views: 1753

Answers (1)

Pankaj Pawar
Pankaj Pawar

Reputation: 345

here is solution i have found

public 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;
}

http://php.net/manual/de/function.array-diff.php

Upvotes: 1

Related Questions