首页 > 解决方案 > 根据值获取多维数组属性

问题描述

我整个早上都在敲打我的大脑,我终于屈服于寻求帮助。

$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_searcharray_column也试过了array_map

有人可以帮我吗?

标签: phpmultidimensional-array

解决方案


这个怎么样:

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

推荐阅读