Use array_diff_assoc () or get the difference in multidimensional arrays

I struggled with what, in my opinion, should be a very simple problem for a while and just can't solve the answer.

I have two arrays, and these arrays contain information about id, linklabel and url in the following format:

$pageids
--------
Array ( 
[0] => Array 
( [id] => 1 
  [linklabel] => Home 
  [url] => home )

[1] => Array 
( [id] => 2 
  [linklabel] => Graphic Design 
  [url] => graphicdesign ) 

[2] => Array 
( [id] => 3 
  [linklabel] => Other Design 
  [url] => otherdesign ) 

[3] => Array 
( [id] => 6 
  [linklabel] => Logo Design 
  [url] => logodesign ) 

[4] => Array 
( [id] => 15 
  [linklabel] => Content Writing 
  [url] => contentwriting ) 
) 


$parentpage
-----------
Array ( 
[0] => Array 
( [id] => 2 
  [linklabel] => Graphic Design 
  [url] => graphicdesign ) 

[1] => Array 
( [id] => 3 
  [linklabel] => Other Design 
  [url] => otherdesign ) ) 

Now I'm trying to compare these two to find the information located in $pageids, but NOT in $parentpage- then another array with the name will be created $pageWithNoChildren. However, when I use the following code:

$pageWithNoChildren = array_diff_assoc($pageids,$parentpage);

array_diff_assoc() , $pageids $parentpages [0] [1], $pageids [2] . , , . , , id, linklabel url $pageids, $parentpages .

array_diff_assoc() , , , [0], [3] [ 4] $pageids?

+4
2

, - :

$pageWithNoChildren = array_map('unserialize',
    array_diff(array_map('serialize', $pageids), array_map('serialize', $parentpage)));
  • array_map() serialize()
  • serialize() - -
  • , , -
  • array_diff()
  • array_map() () unserialize(),

Q.E.D.

+9

@AbraCadaver, , , , , / :

function sortAndSerialize($arr){
    ksort($arr);
    return serialize($arr);
}

array_map('unserialize', array_diff(array_map('sortAndSerialize', $pageids), array_map('sortAndSerialize', $parentpage)));
0

Source: https://habr.com/ru/post/1531355/


All Articles