本文介绍了从数组中递归提取键路径 - 不包括数字键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 php 中,我需要一个函数来返回对象数组中的所有键.对于数组和对象,我需要像 parentkey->key 这样的键.如果键又是一个数组,那么我需要 parentkey->key->key1.

我的代码如下

$array=json_decode('{"client":"4","gateWay":"1","store":"store.shop.com",valid":true",po":34535,additionalPO":23423,customerNotes":","orderItems":[{"item":"123","quantity":10,"supplierLotNo":"","customsValue":"","customsDescription":"","hsCode":""},{项目":345",数量":50}],shippingInfos":[{地址":{城市":钦奈",国家":印度",邮政编码":86715",州":TN",streetAddress1":6971 North Street",streetAddress2":null},联系人":{公司":null,电子邮件":info@st​​ore.com",firstName":test","lastName":"test","phoneNo":null},"ServiceId":"3","thirdPartyAccountNo":"","signatureConfirmation":false,"saturdayDelivery":false}]}',true);函数array_keys_multi(数组$array,$headkey,$pkey){$keys = array();$hkey='';$parentkey='';foreach ($array as $key => $value) {//echo $key;if($headkey!='' && !is_integer($headkey)){if(!is_integer($key)){if (is_array($value) || is_object($value)) {if($pkey!='')$parentkey=$pkey."->".$key;别的$parentkey=$key;foreach($value as $kk=>$val){foreach($val as $kk1=>$val1){$keys[]=$parentkey."->".$kk1;}}//$keys[]=$parentkey."->".$key;}别的{$keys[] = $pkey."->".$key;$hkey=$headkey."->".$key;}}}别的{if(!is_integer($key)){if($pkey!='')$parentkey=$pkey."->".$key;别的$parentkey=$key;if (is_array($value) || is_object($value)) {if($pkey!='')$parentkey=$pkey."->".$key;别的$parentkey=$key;foreach($value as $kk=>$val){//print_r($val);echo "==========";foreach($val as $kk1=>$val1){$keys[]=$parentkey."->".$kk1;}}$hkey=$key;}别的{$keys[]=$key;}}}if (is_array($value) || is_object($value)) {echo $key."-----".$hkey."<br>";$keys = array_merge($keys, array_keys_multi($value,$hkey,$key));}}返回 $keys;}$k=array_keys_multi($array,'','');打印_r($k);

我需要输出为具有以下键路径的数组(注意没有保留数字键):

[客户",网关",商店",有效",宝",额外的PO",客户备注",orderItems->item",订单项->数量","orderItems->supplierLotNo",orderItems->customsValue",orderItems->customsDescription",orderItems->hsCode",shippingInfos->地址->城市",shippingInfos->地址->国家",shippingInfos->地址->邮政编码",shippingInfos->地址->状态",shippingInfos->address->streetAddress1",shippingInfos->地址->streetAddress2",shippingInfos->联系方式->公司",shippingInfos->联系方式->电子邮件",shippingInfos->contact->firstName",shippingInfos->contact->lastName",shippingInfos->contact->phoneNo",shippingInfos->ServiceId",shippingInfos->thirdPartyAccountNo",shippingInfos->signatureConfirmation",shippingInfos->saturdayDelivery"]

我怎样才能做到这一点?

解决方案

这个非常相似的递归代码段开始,我更改键路径分隔符并扩展算法以排除数字键和有孩子的父母.

代码:(演示)

function getUniqueObjectKeyPaths($array, $parentKey = "") {$keys = [];foreach ($array as $parentKey => $v) {如果(is_array($v)){$nestedKeys = getUniqueObjectKeyPaths($v, $parentKey);foreach($nestedKeys as $index => $key) {如果 (!is_numeric($parentKey) && !is_numeric($key)) {$nestedKeys[$index] = $parentKey ."->>.$key;}}array_push($keys, ...$nestedKeys);} elseif (!is_numeric($parentKey)) {$keys[] = $parentKey;}}返回 $keys;}var_export(getUniqueObjectKeyPaths($array));

输出:

数组 (0 =>'客户',1 =>'网关',2 =>'店铺',3 =>'有效的',4 =>'噗',5 =>'附加PO',6 =>'客户备注',7 =>'orderItems->item',8 =>'订单项->数量',9 =>'orderItems->supplierLotNo',10 =>'orderItems->customsValue',11 =>'orderItems->customsDescription',12 =>'orderItems->hsCode',13 =>'orderItems->item',14 =>'订单项->数量',15 =>'shippingInfos->地址->城市',16 =>'shippingInfos->地址->国家',17 =>'shippingInfos->地址->邮政编码',18 =>'shippingInfos->地址->状态',19 =>'shippingInfos->address->streetAddress1',20 =>'shippingInfos->地址->streetAddress2',21 =>'shippingInfos->联系方式->公司',22 =>'shippingInfos->联系方式->电子邮件',23 =>'shippingInfos->contact->firstName',24 =>'shippingInfos-> 联系人-> 姓氏',25 =>'shippingInfos->contact->phoneNo',26 =>'shippingInfos->ServiceId',27 =>'shippingInfos->thirdPartyAccountNo',28 =>'shippingInfos->signatureConfirmation',29 =>'shippingInfos->saturdayDelivery',)

In php, I need a function to return all keys in array of objects. For arrays and objects I need keys like parentkey->key. If the key is again an array, then I need parentkey->key->key1.

My code as below

$array=json_decode('{"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":"[email protected]","firstName":"test",
"lastName":"test","phoneNo":null},"ServiceId":"3","thirdPartyAccountNo":"",
"signatureConfirmation":false,"saturdayDelivery":false}]}',true);

function array_keys_multi(array $array,$headkey,$pkey){
    $keys = array();$hkey='';$parentkey='';
    foreach ($array as $key => $value) {
        //echo $key;
        if($headkey!='' && !is_integer($headkey)){
            if(!is_integer($key)){
                if (is_array($value) || is_object($value)) {
                    if($pkey!='')
                       $parentkey=$pkey."->".$key;
                    else 
                        $parentkey=$key;
                        foreach($value as $kk=>$val){
                            foreach($val as $kk1=>$val1){
                                $keys[]=$parentkey."->".$kk1;
                            }
                        }
                        //$keys[]=$parentkey."->".$key;
                }else{
                    $keys[] = $pkey."->".$key;
                    $hkey=$headkey."->".$key;
                }
            }
        }
        else{
            if(!is_integer($key)){
                if($pkey!='')
                    $parentkey=$pkey."->".$key;
                else 
                    $parentkey=$key;
                if (is_array($value) || is_object($value)) {
                    if($pkey!='')
                        $parentkey=$pkey."->".$key;
                    else
                        $parentkey=$key;
                        foreach($value as $kk=>$val){
                            //print_r($val);echo "==========";
                            foreach($val as $kk1=>$val1){
                                $keys[]=$parentkey."->".$kk1;
                            }
                        }
                        $hkey=$key;

                }else{
                    $keys[]=$key;
                }
            }
        }
        
        if (is_array($value) || is_object($value)) {
            echo $key."-----".$hkey."<br>";
            $keys = array_merge($keys, array_keys_multi($value,$hkey,$key));
        }
    }
    return $keys;
}
$k=array_keys_multi($array,'','');
print_r($k);

I need output as an array with the following key paths (notice no numeric keys are retained):

[
    "client",
    "gateWay",
    "store",
    "valid",
    "po",
    "additionalPO",
    "customerNotes",
    "orderItems->item",
    "orderItems->quantity",
    "orderItems->supplierLotNo",
    "orderItems->customsValue",
    "orderItems->customsDescription",
    "orderItems->hsCode",
    "shippingInfos->address->city",
    "shippingInfos->address->country",
    "shippingInfos->address->postalCode",
    "shippingInfos->address->state",
    "shippingInfos->address->streetAddress1",
    "shippingInfos->address->streetAddress2",
    "shippingInfos->contact->company",
    "shippingInfos->contact->email",
    "shippingInfos->contact->firstName",
    "shippingInfos->contact->lastName",
    "shippingInfos->contact->phoneNo",
    "shippingInfos->ServiceId",
    "shippingInfos->thirdPartyAccountNo",
    "shippingInfos->signatureConfirmation",
    "shippingInfos->saturdayDelivery"
]

How can I achieve this?

解决方案

Starting from this very similar recursive snippet, I changed the key path separator and extended the algorithm to exclude numeric keys and parents with children.

Code: (Demo)

function getUniqueObjectKeyPaths($array, $parentKey = "") {
    $keys = [];
    foreach ($array as $parentKey => $v) {
        if (is_array($v)) {
            $nestedKeys = getUniqueObjectKeyPaths($v, $parentKey);
            foreach($nestedKeys as $index => $key) {
                if (!is_numeric($parentKey) && !is_numeric($key)) {
                    $nestedKeys[$index] = $parentKey . "->" . $key;
                }
            }
            array_push($keys, ...$nestedKeys);
        } elseif (!is_numeric($parentKey)) {
            $keys[] = $parentKey;
        }
    }
    return $keys;
}
var_export(getUniqueObjectKeyPaths($array));

Output:

array (
  0 => 'client',
  1 => 'gateWay',
  2 => 'store',
  3 => 'valid',
  4 => 'po',
  5 => 'additionalPO',
  6 => 'customerNotes',
  7 => 'orderItems->item',
  8 => 'orderItems->quantity',
  9 => 'orderItems->supplierLotNo',
  10 => 'orderItems->customsValue',
  11 => 'orderItems->customsDescription',
  12 => 'orderItems->hsCode',
  13 => 'orderItems->item',
  14 => 'orderItems->quantity',
  15 => 'shippingInfos->address->city',
  16 => 'shippingInfos->address->country',
  17 => 'shippingInfos->address->postalCode',
  18 => 'shippingInfos->address->state',
  19 => 'shippingInfos->address->streetAddress1',
  20 => 'shippingInfos->address->streetAddress2',
  21 => 'shippingInfos->contact->company',
  22 => 'shippingInfos->contact->email',
  23 => 'shippingInfos->contact->firstName',
  24 => 'shippingInfos->contact->lastName',
  25 => 'shippingInfos->contact->phoneNo',
  26 => 'shippingInfos->ServiceId',
  27 => 'shippingInfos->thirdPartyAccountNo',
  28 => 'shippingInfos->signatureConfirmation',
  29 => 'shippingInfos->saturdayDelivery',
)

这篇关于从数组中递归提取键路径 - 不包括数字键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:12