代码之家  ›  专栏  ›  技术社区  ›  YaBCK

基于值获取多维数组属性

  •  0
  • YaBCK  · 技术社区  · 7 年前

    我整个上午都在胡思乱想,最后终于让步寻求帮助。

    $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”的“key”的值

    因此在这个例子中,结果将返回:

    array(
       0 => 06b01b_8675350495a4472ea473f93589258f01~mv2_d_1936_2592_s_2.jpg
    )
    

    我尝试过使用:

    array_search , array_column 也尝试过 array_map

    有人能帮我一把吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Chris    7 年前

    这个怎么样:

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