array_diff的奇怪行为

时间:2016-10-24 11:08:32

标签: php

我正在运行下面的脚本以使用array_diff获取两个数组之间的差异,但我得到的是一个空数组。

$a = [
    "INDEX1" => "No",
    "INDEX2" => "Yes",
    "INDEX3" => "No",
    "INDEX4" => "No"
];

$b = [
    "INDEX1" => "Yes",
    "INDEX2" => "Yes",
    "INDEX3" => "No",
    "INDEX4" => "Yes"
];

print_r( array_diff($a, $b) );

Array
(
)

我不应该这样做吗?

Array
(
    "INDEX1" => "No",
    "INDEX4" => "No"
)

有人可以帮我理解发生了什么吗?

2 个答案:

答案 0 :(得分:2)

结果是预期的:根据manual

  

将array1与一个或多个其他数组进行比较,返回   array1中不存在于任何其他数组中的值

您拥有的唯一Bapplication.conf,两者都存在于这两个数组中。

正如评论中已提到的,您也可以使用build.sbt检查密钥并获得所需的结果。

答案 1 :(得分:1)

请改用extension Object { func toDictionary() -> NSDictionary { let properties = self.objectSchema.properties.map { $0.name } let dictionary = self.dictionaryWithValuesForKeys(properties) let mutabledic = NSMutableDictionary() mutabledic.setValuesForKeysWithDictionary(dictionary) for prop in self.objectSchema.properties as [Property]! { // find lists if let nestedObject = self[prop.name] as? Object { mutabledic.setValue(nestedObject.toDictionary(), forKey: prop.name) } else if let nestedListObject = self[prop.name] as? ListBase { var objects = [AnyObject]() for index in 0..<nestedListObject._rlmArray.count { let object = nestedListObject._rlmArray[index] as AnyObject objects.append(object.toDictionary()) } mutabledic.setObject(objects, forKey: prop.name) } } return mutabledic } } 。你会得到:

array_diff_assoc($a, $b)