php - 根据值获取多维数组属性
问题描述
我整个早上都在敲打我的大脑,我终于屈服于寻求帮助。
$data = '{
"anchorsMap": {
"masterPage": {
"DESKTOP": {
"SITE_HEADER": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_HEADER",
"targetComponent": "masterPage",
"type": "BOTTOM_PARENT"
},
{
"distance": 5,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_HEADER",
"targetComponent": "PAGES_CONTAINER",
"type": "BOTTOM_TOP"
}
],
"PAGES_CONTAINER": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "PAGES_CONTAINER",
"targetComponent": "SITE_FOOTER",
"type": "BOTTOM_TOP"
}
],
"SITE_FOOTER": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_FOOTER",
"targetComponent": "masterPage",
"type": "BOTTOM_PARENT"
}
],
"FvGrdLn5": [
{
"distance": 151,
"locked": false,
"originalValue": 241,
"fromComp": "FvGrdLn5",
"targetComponent": "SITE_STRUCTURE_WRichText_1",
"type": "BOTTOM_TOP"
}
],
"comp-j4ydi86i": [
{
"distance": 21,
"locked": true,
"originalValue": 43,
"fromComp": "comp-j4ydi86i",
"targetComponent": "FvGrdLn0",
"type": "TOP_TOP"
},
{
"distance": 63,
"locked": true,
"originalValue": 85,
"fromComp": "comp-j4ydi86i",
"targetComponent": "FvGrdLn5",
"type": "TOP_TOP"
},
{
"distance": 25,
"locked": true,
"originalValue": 241,
"fromComp": "comp-j4ydi86i",
"targetComponent": "SITE_STRUCTURE_WRichText_1",
"type": "BOTTOM_TOP"
}
],
"SITE_STRUCTURE_WRichText_1": [
{
"distance": 0,
"locked": true,
"originalValue": 241,
"fromComp": "SITE_STRUCTURE_WRichText_1",
"targetComponent": "WRchTxt1",
"type": "TOP_TOP"
},
{
"distance": 4,
"locked": true,
"originalValue": 263,
"fromComp": "SITE_STRUCTURE_WRichText_1",
"targetComponent": "SITE_FOOTER",
"type": "BOTTOM_PARENT"
}
],
"WRchTxt1": [
{
"distance": 5,
"locked": true,
"originalValue": 263,
"fromComp": "WRchTxt1",
"targetComponent": "SITE_FOOTER",
"type": "BOTTOM_PARENT"
}
],
"SITE_PAGES": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_PAGES",
"targetComponent": "PAGES_CONTAINER",
"type": "BOTTOM_PARENT"
}
],
"comp-ilxrenwr": [
{
"distance": 19,
"locked": true,
"originalValue": 237,
"fromComp": "comp-ilxrenwr",
"targetComponent": "comp-ilxrftyg",
"type": "BOTTOM_TOP"
}
]
}
},
"defaultMasterPage": {
"DESKTOP": {
"SITE_HEADER": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_HEADER",
"targetComponent": "masterPage",
"type": "BOTTOM_PARENT"
},
{
"distance": 5,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_HEADER",
"targetComponent": "PAGES_CONTAINER",
"type": "BOTTOM_TOP"
}
],
"comp-j4ydi86i": [
{
"distance": 21,
"locked": true,
"originalValue": 43,
"fromComp": "comp-j4ydi86i",
"targetComponent": "FvGrdLn0",
"type": "TOP_TOP"
},
{
"distance": 63,
"locked": true,
"originalValue": 85,
"fromComp": "comp-j4ydi86i",
"targetComponent": "FvGrdLn5",
"type": "TOP_TOP"
},
{
"distance": 25,
"locked": true,
"originalValue": 241,
"fromComp": "comp-j4ydi86i",
"targetComponent": "SITE_STRUCTURE_WRichText_1",
"type": "BOTTOM_TOP"
}
],
"SITE_PAGES": [
{
"distance": 0,
"locked": true,
"originalValue": 0,
"fromComp": "SITE_PAGES",
"targetComponent": "PAGES_CONTAINER",
"type": "BOTTOM_PARENT"
}
]
}
},
"c1adf": {
"DESKTOP": {
},
"dataItem-jaifm1zz2": {
"type": "Image",
"id": "dataItem-jaifm1zz2",
"metaData": {
"isPreset": false,
"schemaVersion": "2.0",
"isHidden": false
},
"title": "IMG_5774",
"uri": "06b01b_8675350495a4472ea473f93589258f01~mv2_d_1936_2592_s_2.jpg",
"width": 1936,
"height": 2592,
"alt": ""
}
}
}
}';
$data = json_decode($data, true);
我正在尝试获取所有具有等于“uri”的“键”的值
所以在这个例子中,结果将返回:
array(
0 => 06b01b_8675350495a4472ea473f93589258f01~mv2_d_1936_2592_s_2.jpg
)
我试过使用:
array_search
,array_column
也试过了array_map
有人可以帮我吗?
解决方案
这个怎么样:
$matches = [];
$identifier = 'uri';
array_walk_recursive($data, function ($item, $key) use (&$matches, $identifier) {
if ($key == $identifier) {
return $matches[] = $item;
}
});
print_r($matches);
Array ( [0] => 06b01b_8675350495a4472ea473f93589258f01~mv2_d_1936_2592_s_2.jpg )
推荐阅读
- ansible-inventory - 一个密码失败时的ansible库存尝试不同
- ios - TextField 在其他视图中通过协议更改值,如果用户更改 textField 输入如何存储值
- react-native - React Native Fetch data from api issue
- node.js - heroku push 卡住了
- powershell - 我正在做 3 个连续的 foreach 循环,它花费了很多时间?有没有更好的方法来解决这个难题?
- javascript - 使用 Express 异步错误的 Stange 行为
- javascript - JavaScript/DOM:MutationObserver 检测 DOM 树中任何位置的 `lang` 属性的变化
- unit-testing - 如何在 test.check 中生成随机电子邮件地址?
- html - 如何使用flexbox删除div之间的空白
- python - 模型表单中 ChoiceField 的小部件