使用 array_diff_assoc() 或获取多维数组的差异 [英] Use array_diff_assoc() or get difference of multidimensional arrays

查看:29
本文介绍了使用 array_diff_assoc() 或获取多维数组的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在为我认为应该是一个非常简单的问题而苦苦挣扎,只是无法找到答案.

I've been struggling with what I think should be a really easy issue for sometime now and just can't work out the answer.

我有两个数组,这些数组包含有关 id、linklabel 和 url 的信息,格式如下:

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 ) ) 

我现在正在尝试比较这两个以找到 $pageids 中但不在 $parentpage 中的信息 - 这将构成另一个数组称为 $pageWithNoChildren.但是,当我使用以下代码时:

I'm now trying to compare these two in order to find the information that is in $pageids but NOT in $parentpage - this will then make up another array called $pageWithNoChildren. However when I use the following code:

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

array_diff_assoc() 在数组的第一层运行,因此看到 $pageids$parentpages 都有一个 [0] 和 [1] 键,因此它会忽略它们并从 [2] 开始返回 $pageids 中的所有信息.但是我希望它查看嵌套数组的内容并比较它们,例如我需要它来查看哪些 id、linklabel 和 url 在 $pageids 中而不是在 $parentpages 中并返回这些值.

The array_diff_assoc() runs on the first level of the arrays and therefore sees that both $pageids and $parentpages have a [0] and [1] key so it ignores them and returns all the information from $pageids from [2] onwards. However I want it to look at the content of the nested arrays and compare those e.g. I need it to see which id, linklabel and url are in $pageids and not in $parentpages and return those values.

我怎样才能让 array_diff_assoc() 在嵌套数组的键上运行,而不是在第一个数组的键上运行,所以最终结果是一个包含 [0]、[3] 和 [4] 数组来自 $pageids?

How can I get the array_diff_assoc() to run on the keys of the nested arrays and not the keys of the first arrays so the final result is an array that contains the contents of the [0], [3] and [4] arrays from $pageids?

推荐答案

要检查多部门,请尝试以下操作:

To check multi-deminsions try something like this:

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

  • array_map() 通过 serialize() 运行主数组的每个子数组,将每个子数组转换为该子数组的字符串表示
    • 主数组现在的值不是数组而是子数组的字符串表示
      • array_map() runs each sub-array of the main arrays through serialize() which converts each sub-array into a string representation of that sub-array
        • the main arrays now have values that are not arrays but string representations of the sub-arrays
        • Q.E.D.

          这篇关于使用 array_diff_assoc() 或获取多维数组的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆