0

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

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

1 Answers1

0

This is what I got (not sure if it's what you want)

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

Output ['contacts','emails','nest_uid']

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

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

)

Sandbox

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • no . That's not what i want . Well Param decides the path . And i need to return the elements alont with there nestuid of there parents. – Vikram Anand Bhushan Sep 19 '18 at 05:47
  • For example . If the path is until emails . So we should be getting two emails out of the the dataset and uidlist should have nestUId of root - contact - email – Vikram Anand Bhushan Sep 19 '18 at 05:48