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

查看:499
本文介绍了使用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]键,因此它将忽略它们并从$pageids返回所有信息从[2]开始.但是我希望它查看嵌套数组的内容并进行比较我需要它查看$pageids而不是$parentpages中的id,linklabel和url,然后返回这些值.

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()
  • 运行主数组的每个子数组
  • serialize()将每个子数组转换为该子数组的字符串表示形式
  • 主数组现在具有的值不是数组,而是子数组的字符串表示形式
  • array_diff()现在为每个要比较的数组都有一个一维数组
  • 返回差值后array_map()通过unserialize()运行数组结果(差值)以将字符串表示形式转换回子数组
    • array_map() runs each sub-array of the main arrays through serialize()
    • serialize() 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
    • array_diff() now has a one-dimensional array for each of the arrays to compare
    • after the difference is returned array_map() runs the array result (differences) through unserialize() to turn the string representations back into sub-arrays
    • Q.E.D.

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

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