我可以在php中获取多级关联数组的所有键吗 [英] Can I get all keys of an multi level associative arrays in php

查看:46
本文介绍了我可以在php中获取多级关联数组的所有键吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php中有什么方法可以获取php中多维数组的n级键?

这是我的关联数组,作为输出,我需要一个包含键 object_id 的所有值的数组,因为您可以从结构本身中找到许多 object_id ,那么如何获取键 object_id 的所有值?

 数组'购物车'=>大批12061 =>大批'object_id'=>字符串'12061'(长度= 5)'discriminator'=>字符串'SimpleProductOffering'(长度= 21)'spec'=>大批100012061 =>大批'object_id'=>字符串'100012061'(长度= 9)'discriminator'=>字符串"CompositeProductSpecification"(长度= 29)'特征'=>大批'MAIN_CPE'=>大批'object_id'=>字符串'1000000000015'(长度= 13)'is_configurable'=>字符串'0'(长度= 1)'trait_value'=>大批10001 =>大批'object_id'=>字符串'10001'(长度= 5)'抵押'=>大批空的'抵押'=>大批空的'offer_type'=>空值'价格'=>大批12862 =>大批'object_id'=>字符串'12862'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的12876 =>大批'object_id'=>字符串'12876'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批0 =>字符串'12876'(长度= 5)1 =>字符串'12894'(长度= 5)'抵押'=>大批空的'contained_offers'=>大批空的'family'=>大批0 =>字符串电视订阅"(长度= 24)'relationship'=>大批'儿童'=>大批'object_id'=>字符串'1206102000'(长度= 10)'min'=>字符串'0'(长度= 1)'max'=>字符串'1000000'(长度= 7)'related_offer'=>大批0 =>字符串'10410'(长度= 5)1 =>字符串'10411'(长度= 5)'REQUIREMENTS'=>大批'object_id'=>字符串'1206104000'(长度= 10)'min'=>字符串'1'(长度= 1)'max'=>字符串'1'(长度= 1)'related_offer'=>大批0 =>字符串'11950'(长度= 5)1 =>字符串'11990'(长度= 5)'EXCLUSIONS'=>大批'object_id'=>字符串'1206101000'(长度= 10)'min'=>字符串'1'(长度= 1)'max'=>字符串'1'(长度= 1)'related_offer'=>大批0 =>字符串'12062'(长度= 5)1 =>字符串'12063'(长度= 5)'ALTERNATIVES'=>大批'object_id'=>字符串'1206105000'(长度= 10)'min'=>字符串'1'(长度= 1)'max'=>字符串'1'(长度= 1)'related_offer'=>大批0 =>字符串'12263'(长度= 5)'BUNDLE_ITEMS'=>大批'object_id'=>字符串'1206106000'(长度= 10)'min'=>字符串'1'(长度= 1)'max'=>字符串'1'(长度= 1)'related_offer'=>大批0 =>字符串'12062'(长度= 5)'financial_terms'=>大批'billing_period'=>大批0 =>字符串'QUARTERLY'(长度= 9)'payment_method'=>大批0 =>字符串'DIRECT_DEBIT'(长度= 12)'bill_presentation'=>大批0 =>字符串'PAPER'(长度= 5)'contract_constraints'=>大批'min_contract_period'=>整数24'cancellation_period'=>字符串'ALWAYS'(长度= 6)'notice_period'=>诠释3'rollover_period'=>诠释2'right_of_wd_period'=>整数1'抵押'=>大批空的10017 =>大批'object_id'=>字符串'10017'(长度= 5)'spec'=>大批100010017 =>大批'object_id'=>字符串'100010017'(长度= 9)'discriminator'=>字符串"CompositeProductSpecification"(长度= 29)'特征'=>大批空的'offer_type'=>空值'价格'=>大批300306 =>大批'object_id'=>字符串'300306'(长度= 6)'price_alteration'=>大批空的'抵押'=>大批空的12894 =>大批'object_id'=>字符串'12894'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的12862 =>大批'object_id'=>字符串'12862'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的12876 =>大批'object_id'=>字符串'12876'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批0 =>字符串'12876'(长度= 5)1 =>字符串'12894'(长度= 5)'抵押'=>大批空的'contained_offers'=>大批0 =>空值'family'=>大批空的'relationship'=>大批'EXCLUSIONS'=>大批'object_id'=>字符串'1001701000'(长度= 10)'min'=>字符串'1'(长度= 1)'max'=>字符串'1'(长度= 1)'related_offer'=>大批0 =>字符串'11893'(长度= 5)14 =>字符串'12305'(长度= 5)15 =>字符串'12306'(长度= 5)'financial_terms'=>大批'billing_period'=>大批0 =>字符串"MONTHLY"(长度= 7)1 =>字符串'QUARTERLY'(长度= 9)'payment_method'=>大批0 =>字符串'DIRECT_DEBIT'(长度= 12)1 =>字符串'DIRECT_DEBIT'(长度= 12)'bill_presentation'=>大批0 =>字符串"EMAIL"(长度= 5)1 =>字符串'PAPER'(长度= 5)'contract_constraints'=>大批'min_contract_period'=>整数24'cancellation_period'=>字符串'ALWAYS'(长度= 6)'notice_period'=>诠释3'rollover_period'=>诠释2'right_of_wd_period'=>整数1'抵押'=>大批空的0 =>大批11990 =>大批'object_id'=>字符串'11990'(长度= 5)'discriminator'=>字符串'SimpleProductOffering'(长度= 21)'spec'=>大批100011990 =>大批'object_id'=>字符串'100011990'(长度= 9)'discriminator'=>字符串"CompositeProductSpecification"(长度= 29)'特征'=>大批空的'offer_type'=>空值'价格'=>大批12862 =>大批'object_id'=>字符串'12862'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的12876 =>大批'object_id'=>字符串'12876'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批10017 =>大批'object_id'=>字符串'10017'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的12894 =>大批'object_id'=>字符串'12894'(长度= 5)'discriminator'=>字符串'RecurringChargeProdOfferPriceCharge'(长度= 35)'price_alteration'=>大批空的'抵押'=>大批空的'抵押'=>大批空的'contained_offers'=>大批空的'family'=>大批0 =>字符串'CATV'(长度= 4)'relationship'=>大批空的'financial_terms'=>大批'billing_period'=>大批0 =>字符串'QUARTERLY'(长度= 9)'payment_method'=>大批0 =>字符串'DIRECT_DEBIT'(长度= 12)'bill_presentation'=>大批0 =>字符串'PAPER'(长度= 5)'contract_constraints'=>大批'min_contract_period'=>整数24'抵押'=>大批空的 

输出应该是一个包含键= object_id 的所有值的数组.请告知?

我猜,由于Dereleased的解决方案会更快(因为它使用内部循环),因此我的还可以处理 object_id 键的数组值.您的权衡;)


  function find_all($ needle,array $ haystack,array& $ result = null){//这是初始化结果数组,仅用于//此函数的第一个调用if(is_null($ result)){$ result = array();}foreach($ haystack as $ key => $ value){//检查键是否是我们要寻找的值.如果值//不是数组,请将其添加到结果数组.if($ key === $ needle&&!is_array($ value)){$ result [] = $ value;}if(is_array($ value)){//如果当前值为数组,则执行相同的操作//使用此子数组"进行操作.find_all($ needle,$ value,$ result);}}//只有在第一个函数调用中才需要检索结果返回$ result;} 

如您所见,结果数组以引用的形式提供给函数的每个调用(由& 表示).这样,此函数的每个递归调用都可以访问相同数组,并且只能添加查找.

您可以这样做:

  $ values = find_all('object_id',$ array); 

它为您提供了我的阵列:

 数组([0] =>12061[1] =>100012061[2] =>1000000000015[3] =>10001[4] =>12862[5] =>12876[6] =>1206102000[7] =>1206104000[8] =>1206101000[9] =>1206105000[10] =>1206106000[11] =>10017[12] =>100010017[13] =>300306[14] =>12894[15] =>12862[16] =>12876[17] =>1001701000[18] =>11990[19] =>100011990[20] =>12862[21] =>12876[22] =>10017[23] =>12894) 

Is there any way in php wherein I can get n level keys for multi-dimensional array in php ?

Here is my associative array and as output I want an array which would contain all the values for keys object_id as you can there from the structure itself there are many nested levels for object_id and so how can I get all the values for keys object_id ?

   array
  'cart' => 
    array
      12061 => 
        array
          'object_id' => string '12061' (length=5)
          'discriminator' => string 'SimpleProductOffering' (length=21)
          'spec' => 
            array
              100012061 => 
                array
                  'object_id' => string '100012061' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      'MAIN_CPE' => 
                        array
                          'object_id' => string '1000000000015' (length=13)
                          'is_configurable' => string '0' (length=1)
                          'trait_value' => 
                            array
                              10001 => 
                                array
                                  'object_id' => string '10001' (length=5)
                                  'collateral' => 
                                    array
                                      empty
                          'collateral' => 
                            array
                              empty
          'offer_type' => null
          'price' => 
            array
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      0 => string '12876' (length=5)
                      1 => string '12894' (length=5)
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              empty
          'family' => 
            array
              0 => string 'TV Subscription' (length=24)
          'relationship' => 
            array
              'CHILDREN' => 
                array
                  'object_id' => string '1206102000' (length=10)
                  'min' => string '0' (length=1)
                  'max' => string '1000000' (length=7)
                  'related_offer' => 
                    array
                      0 => string '10410' (length=5)
                      1 => string '10411' (length=5)
              'REQUIREMENTS' => 
                array
                  'object_id' => string '1206104000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '11950' (length=5)
                      1 => string '11990' (length=5)
              'EXCLUSIONS' => 
                array
                  'object_id' => string '1206101000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12062' (length=5)
                      1 => string '12063' (length=5)
              'ALTERNATIVES' => 
                array
                  'object_id' => string '1206105000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12263' (length=5)
              'BUNDLE_ITEMS' => 
                array
                  'object_id' => string '1206106000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '12062' (length=5)
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
              'cancellation_period' => string 'ALWAYS' (length=6)
              'notice_period' => int 3
              'rollover_period' => int 2
              'right_of_wd_period' => int 1
          'collateral' => 
            array
              empty
      10017 => 
        array
          'object_id' => string '10017' (length=5)
          'spec' => 
            array
              100010017 => 
                array
                  'object_id' => string '100010017' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      empty
          'offer_type' => null
          'price' => 
            array
              300306 => 
                array
                  'object_id' => string '300306' (length=6)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12894 => 
                array
                  'object_id' => string '12894' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      0 => string '12876' (length=5)
                      1 => string '12894' (length=5)
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              0 => null
          'family' => 
            array
              empty
          'relationship' => 
            array
              'EXCLUSIONS' => 
                array
                  'object_id' => string '1001701000' (length=10)
                  'min' => string '1' (length=1)
                  'max' => string '1' (length=1)
                  'related_offer' => 
                    array
                      0 => string '11893' (length=5)
                      14 => string '12305' (length=5)
                      15 => string '12306' (length=5)
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'MONTHLY' (length=7)
                  1 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
                  1 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'EMAIL' (length=5)
                  1 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
              'cancellation_period' => string 'ALWAYS' (length=6)
              'notice_period' => int 3
              'rollover_period' => int 2
              'right_of_wd_period' => int 1
          'collateral' => 
            array
              empty
  0 => 
    array
      11990 => 
        array
          'object_id' => string '11990' (length=5)
          'discriminator' => string 'SimpleProductOffering' (length=21)
          'spec' => 
            array
              100011990 => 
                array
                  'object_id' => string '100011990' (length=9)
                  'discriminator' => string 'CompositeProductSpecification' (length=29)
                  'trait' => 
                    array
                      empty
          'offer_type' => null
          'price' => 
            array
              12862 => 
                array
                  'object_id' => string '12862' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      empty
                  'collateral' => 
                    array
                      empty
              12876 => 
                array
                  'object_id' => string '12876' (length=5)
                  'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                  'price_alteration' => 
                    array
                      10017 => 
                        array
                          'object_id' => string '10017' (length=5)
                          'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                          'price_alteration' => 
                            array
                              empty
                          'collateral' => 
                            array
                              empty
                      12894 => 
                        array
                          'object_id' => string '12894' (length=5)
                          'discriminator' => string 'RecurringChargeProdOfferPriceCharge' (length=35)
                          'price_alteration' => 
                            array
                              empty
                          'collateral' => 
                            array
                              empty
                  'collateral' => 
                    array
                      empty
          'contained_offers' => 
            array
              empty
          'family' => 
            array
              0 => string 'CATV' (length=4)
          'relationship' => 
            array
              empty
          'financial_terms' => 
            array
              'billing_period' => 
                array
                  0 => string 'QUARTERLY' (length=9)
              'payment_method' => 
                array
                  0 => string 'DIRECT_DEBIT' (length=12)
              'bill_presentation' => 
                array
                  0 => string 'PAPER' (length=5)
          'contract_constraints' => 
            array
              'min_contract_period' => int 24
          'collateral' => 
            array
              empty

Output should be an array which would contain all the values for keys = object_id. Kindly advise ?

解决方案

Dereleased's solution will be faster I guess (as it uses internal loops), mine can also deal with array values for object_id keys. Your tradeoff ;)


function find_all($needle, array $haystack, array &$result = null) {
    // This is to initialize the result array and is only needed for
    // the first call of this function
    if(is_null($result)) {
        $result = array();
    }
    foreach($haystack as $key => $value) {
        // Check whether the key is the value we are looking for. If the value
        // is not an array, add it to the result array.
        if($key === $needle && !is_array($value)) {
            $result[] = $value;
        }
        if(is_array($value)) {
            // If the current value is an array, we perform the same
            // operation with this 'subarray'.
            find_all($needle, $value, $result);
        }
    }
    // This is only needed in the first function call to retrieve the results
    return $result;
}

As you can see, the result array is given to every call of the function as reference (denoted by the &). This way, every recursive call of this function has access to the same array and can just add a find.

You can do:

$values = find_all('object_id', $array);

It gives me for your array:

Array
(
    [0] => 12061
    [1] => 100012061
    [2] => 1000000000015
    [3] => 10001
    [4] => 12862
    [5] => 12876
    [6] => 1206102000
    [7] => 1206104000
    [8] => 1206101000
    [9] => 1206105000
    [10] => 1206106000
    [11] => 10017
    [12] => 100010017
    [13] => 300306
    [14] => 12894
    [15] => 12862
    [16] => 12876
    [17] => 1001701000
    [18] => 11990
    [19] => 100011990
    [20] => 12862
    [21] => 12876
    [22] => 10017
    [23] => 12894
)

这篇关于我可以在php中获取多级关联数组的所有键吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆