如何解析json并返回源树

时间:2018-09-18 15:16:33

标签: php laravel-5

我有一个看起来像这样的JSON

$data = '{
    "account_owner": "",
    "account_type": "",
    "nest_uid":"17_15_1536914882_yhHDzQsDSI",
    "business_name": "",
    "sync_block": false,
    "validation": {"isError": false,
    "inputList": [],
    "message": ""},
    "contacts": [
        {
            "con_title": "",
            "con_fName": "",
            "con_lName": "",
            "con_job_title": "",
            "emails": [
                {
                    "email": "",
                    "type": "",
                    "primary": false,
                    "nest_uid": "17_15_1536914882_yhHDzQsDSK",
                    "validation": {
                        "isError": false,
                        "inputList": [],
                        "message": ""
                    },
                    "checked": false
                    }
            ],
            "phones": [
                {
                    "phone": "",
                    "type": "",
                    "primary": false,
                    "nest_uid": "17_15_1536914882_uHN38SxJ3s",
                    "validation": {
                        "isError": false,
                        "inputList": [],
                        "message": ""
                    },
                    "checked": false
                }
            ],
            "nest_uid": "17_15_1536914882_hwzB7dIn9v",
            "checked": false
        },
        {
        "con_title": "",
        "con_fName": "",
        "con_lName": "",
        "con_job_title": "",
        "emails": [
            {
                "email": "",
                "type": "",
                "primary": false,
                "nest_uid": "17_15_1536914882_yhHDzQsDSx",
                "validation": {
                    "isError": false,
                    "inputList": [],
                    "message": ""
                },
                "checked": false
            }
        ],
        "phones": [
            {
                "phone": "",
                "type": "",
                "primary": false,
                "nest_uid": "17_15_1536914882_uHN38SxJ3Y",
                "validation": {
                    "isError": false,
                    "inputList": [],
                    "message": ""
                },
                "checked": false
            }
        ],
        "nest_uid": "17_15_1536914882_hwzB7dIn9x",
        "checked": false
        }
    ]
}';

然后我还有另一个输入

$param = ['contacts', 'emails'];

我必须编写一个递归函数,该函数将$param作为路径并遍历数据集以提供类似这样的输出

例如,如果路径为

["contacts"]

输出

[
  {
    "data": {
      "con_title": "",
      "con_fName": "",
      "con_lName": "",
      "con_job_title": "",
      "emails": [
        {
          "email": "",
          "type": "",
          "primary": false,
          "nest_uid": "17_15_1536914882_yhHDzQsDSK",
          "validation": {
            "isError": false,
            "inputList": [],
            "message": ""
          },
          "checked": false
        }
      ],
      "phones": [
        {
          "phone": "",
          "type": "",
          "primary": false,
          "nest_uid": "17_15_1536914882_uHN38SxJ3s",
          "validation": {
            "isError": false,
            "inputList": [],
            "message": ""
          },
          "checked": false
        }
      ],
      "nest_uid": "17_15_1536914882_hwzB7dIn9v",
      "checked": false
    },
    "source": [
      "17_15_1536914882_yhHDzQsDSI",
      "17_15_1536914882_yhHDzQsDSK"
    ]
  },
  {
    "data": {
      "con_title": "",
      "con_fName": "",
      "con_lName": "",
      "con_job_title": "",
      "emails": [
        {
          "email": "",
          "type": "",
          "primary": false,
          "nest_uid": "17_15_1536914882_yhHDzQsDSK",
          "validation": {
            "isError": false,
            "inputList": [],
            "message": ""
          },
          "checked": false
        }
      ],
      "phones": [
        {
          "phone": "",
          "type": "",
          "primary": false,
          "nest_uid": "17_15_1536914882_uHN38SxJ3s",
          "validation": {
            "isError": false,
            "inputList": [],
            "message": ""
          },
          "checked": false
        }
      ],
      "nest_uid": "17_15_1536914882_hwzB7dIn9v",
      "checked": false
    },
    "source": [
      "17_15_1536914882_yhHDzQsDSI",
      "17_15_1536914882_yhHDzQsDSx"
    ]
  }
]

所以基本上,与那里的源树匹配的元素的数目是nest_ui

的列表

我正在尝试按照这些思路编写代码

function genrate_nested_uid($innerdata,$param,$nestUidList,$parentkey){

    foreach($param as $keyParam)
    {
        foreach($innerdata as $key=>$property)
        {
           if($key=="nest_uid")
           {
                $parentkey[] = $property;
           }
           else if(is_array($property) && $key==$keyParam)
                {


                    array_shift($param);
                    if(count($param)>0)
                    {

                        foreach($property as $innerproperty){genrate_nested_uid($innerproperty,$param,$nestUidList,$parentkey);

                        }
                    }
                    else{
                        $nestUidList[] = [
                            "data"=>$property,
                            "list"=>$parentkey
                            ];
                    }

                }
        }

    }
        // echo 
    return $nestUidList;  

  }
$nestUidList=[];$parentkey=[];

$innerdataX = json_decode($data);
$paramX = ['contacts','emails'];
$res = genrate_nested_uid($innerdataX,$paramX,$nestUidList,$parentkey);

但是我没有得到想要的结果

1 个答案:

答案 0 :(得分:0)

这就是我得到的(不确定是否是您想要的)

$data = '{"account_owner": "","account_type": "","nest_uid":"17_15_1536914882_yhHDzQsDSI","business_name": "","sync_block": false,"validation": {"isError": false,"inputList": [],"message": ""},
"contacts": [{"con_title": "","con_fName": "","con_lName": "","con_job_title": "","emails": [{"email": "","type": "","primary": false,"nest_uid": "17_15_1536914882_yhHDzQsDSK","validation": {"isError": false,
"inputList": [],"message": ""},"checked": false}],"phones": [{"phone": "","type": "","primary": false,
"nest_uid": "17_15_1536914882_uHN38SxJ3s","validation": {"isError": false,"inputList": [],"message": ""
},"checked": false}],"nest_uid": "17_15_1536914882_hwzB7dIn9v","checked": false},{"con_title": "","con_fName": "","con_lName": "","con_job_title": "","emails": [{"email": "","type": "","primary": false,"nest_uid": "17_15_1536914882_yhHDzQsDSx","validation": {"isError": false,
"inputList": [],"message": ""},"checked": false}],"phones": [{"phone": "","type": "","primary": false,
"nest_uid": "17_15_1536914882_uHN38SxJ3Y","validation": {"isError": false,"inputList": [],"message": ""
},"checked": false}],"nest_uid": "17_15_1536914882_hwzB7dIn9x","checked": false}]}';

$data = json_decode($data, true);

$d = [];
$param = ['contacts','emails','nest_uid'];

function walk($data, $param){
    $search = current($param);
    if(!$search) return $data;
    $out = [];
    foreach($data as $key=>$value){
        if(is_numeric($key)){
            $out[] = walk($value,$param);  
        }else if($key == $search && count($param)){
            array_shift($param);
            $out = walk($value, $param);
        }
    }
    return $out;
}

print_r(walk($data, $param));

输出['contacts','emails','nest_uid']

Array(
[0] => Array
    (
        [0] => 17_15_1536914882_yhHDzQsDSK
    )

[1] => Array
    (
        [0] => 17_15_1536914882_yhHDzQsDSx
    )

)

Sandbox