因此,我从JSON文件中获取信息,并得到一个名为“ items”的数组,并将其传递给PHP中的该数组:
"items": [
{
"id": "2",
"parent": null,
"itemType": 0,
"title": "Manuel Perez",
"description": "Chief Executive Officer (CEO)",
"phone": "(491) 183-55-45",
"email": "[email protected]",
"photo": "",
"image": "demo/images/photos/m.png",
"href": "showdetails.php?recordid=2",
"isVisible": true
}
然后,我必须将其与可以包含更多或更少项目的另一个进行比较,但是我可以采用那些可以改变数组大小的元素。但是当我想要在大小相同且键值不同而我无法使其工作时获得差异时。
如果在位置2的第二个数组中,“ itemType”的值更改为1,我想得到类似以下内容:
Array[2] - > "itemType": 1
这样,我就可以知道是否发生了某些变化。
假设您有两个数组$ items1和$ items2:
$items1 = [
"id" => "2",
"itemType" => 0,
];
$items2 = [
"parent" => null,
"itemType" => 1,
];
要获得与$ item1相比在$ items2中添加或修改的内容,可以使用:
array_diff_assoc($items2, $items1) // displays ['parent' => null, 'itemType' => 1]
要获得与$ item2相比在$ items1中添加或修改的内容,可以使用:
array_diff_assoc($items1, $items2) // displays ['id' => "2", 'itemType' => 0]
如果两者都需要,则可以组合这两个数组,并根据需要使用它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句