I have a JSON which looks like this
$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
}
]
}';
then I have another input
$param = ['contacts', 'emails'];
I have to write a recursive function which will take $param
as path and traverse the dataset to give an output something like this
For example, if the path is
["contacts"]
Output
[
{
"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"
]
}
]
So basically the number of element matched along with there source tree, which is the list of nest_ui
I am trying to write code along these lines
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);
But I am not getting the desired result