Please refer my question,
Recursively extract key paths from array - excluding numeric keys
<?php
$array=json_decode('{"orders": [
{"client":"4","gateWay":"1","store":"store.shop.com","valid":"true","po":34535,"additionalPO":23423,"customerNotes":"","orderItems":[{"item":"123","quantity":10,"supplierLotNo":"","customsValue":"","customsDescription":"","hsCode":""},{"item":"345","quantity":50}],"shippingInfos":[{"address":{"city":"Chennai","country":"India","postalCode":"86715","state":"TN","streetAddress1":"6971 North Street","streetAddress2":null},"contact":{"company":null,"email":"info@store.com","firstName":"test","lastName":"test","phoneNo":null},"ServiceId":"3","thirdPartyAccountNo":"","signatureConfirmation":false,"saturdayDelivery":false}]}]}',true);
function getUniqueObjectKeyPaths(array $array, $parentKey = "") {
$keys = [];
foreach ($array as $parentKey => $v) {
if (!is_numeric($parentKey) && !is_array($v)) {
$keys[] = $parentKey;
}
if (is_array($v)) {
$nestedKeys = getUniqueObjectKeyPaths($v, $parentKey);
foreach($nestedKeys as $index => $key) {
if (!is_numeric($parentKey) && !is_numeric($key)) {
$nestedKeys[$index] = $parentKey . "->" . $key;
}
}
$keys = array_merge($keys, $nestedKeys);
}
}
return $keys;
}
$k=getUniqueObjectKeyPaths($array);
print_r($k);
?>
I changed the above function to work for my requirement,
function getUniqueObjectKeyPaths(array $array, $parentKey = "") {
$keys = [];
$i=0;
foreach ($array as $parentKey => $v) {
if (!is_numeric($parentKey) && !is_array($v)) {
$keys[] = $parentKey;
}
if (is_array($v)) {
if($i==0)
$parentKey="";
$nestedKeys = getUniqueObjectKeyPaths($v, $parentKey);
foreach($nestedKeys as $index => $key) {
if (!is_numeric($parentKey) && !is_numeric($key)) {
$nestedKeys[$index] = $parentKey . "->" . $key;
$keys[] = $parentKey;
}
}
$keys = array_merge($keys, $nestedKeys);
}
$i++;
}
return $keys;
}
my output is,
Array ( [0] => orders->client [1] => orders->gateWay [2] => orders->store [3] => orders->valid [4] => orders->po [5] => orders->additionalPO [6] => orders->customerNotes [7] => orders->orderItems->item [8] => orders->orderItems->quantity [9] => orders->orderItems->supplierLotNo [10] => orders->orderItems->customsValue [11] => orders->orderItems->customsDescription [12] => orders->orderItems->hsCode [13] => orders->orderItems->item [14] => orders->orderItems->quantity [15] => orders->shippingInfos->address->city [16] => orders->shippingInfos->address->country [17] => orders->shippingInfos->address->postalCode [18] => orders->shippingInfos->address->state [19] => orders->shippingInfos->address->streetAddress1 [20] => orders->shippingInfos->address->streetAddress2 [21] => orders->shippingInfos->contact->company [22] => orders->shippingInfos->contact->email [23] => orders->shippingInfos->contact->firstName [24] => orders->shippingInfos->contact->lastName [25] => orders->shippingInfos->contact->phoneNo [26] => orders->shippingInfos->ServiceId [27] => orders->shippingInfos->thirdPartyAccountNo [28] => orders->shippingInfos->signatureConfirmation [29] => orders->shippingInfos->saturdayDelivery )
But my expected output is,
I need orders as one key,instead of orders->shippingInfos->saturdayDelivery i need only shippingInfos->saturdayDelivery.ie. skip first parent if second parent is array or object.
how can i achieve this,