array difference

前端 未结 3 1353
闹比i
闹比i 2021-01-24 21:19

I need to get the difference between these two arrays, I\'ve tried array_diff($array1,$array2) without success, any idea?

array1

Array
(
            


        
相关标签:
3条回答
  • 2021-01-24 21:53

    Maybe I'm misunderstanding, but can't you just do something like this for your specific problem?

    $newStatuses = array();
    
    foreach($array1 as $element1) {
        foreach($array2 as $element2) {
            if($element1['status'] == $element2['status']) {
                continue 2;
            }
        }
    
        $newStatuses[] = $element1;
    }
    

    Each element of $newStatuses will be an array with a 'status' element from array1 that was not in array2.

    So, $newStatuses would be this:

    Array
    (
        [0] => Array
            (
                [status] => 61192106047320064
            )
    
    )
    
    0 讨论(0)
  • 2021-01-24 21:55

    have a look at this code, its part of cakephp but you may be able to adapt / rip it out

    https://github.com/cakephp/cakephp/blob/master/cake/libs/set.php#L792

    and the docs

    http://book.cakephp.org/view/1496/diff

    0 讨论(0)
  • 2021-01-24 22:02

    According to array_diff,

    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]);.

    Therefore you cannot diff the second dimension of these arrays directly.

    Instead, maybe you can extract the status values with array_map, save as two 1-dimensional arrays, and then array_diff. If you have multiple keys, use a for loop.

    0 讨论(0)
提交回复
热议问题