我可以在php中获取多级关联数组的所有键吗
在 php 中是否有任何方法可以获取 php 中多维数组的 n 级键?
这是我的关联数组,作为输出,我想要一个包含键 object_id
的所有值的数组,因为您可以从结构本身那里看到 object_id
有许多嵌套级别那么如何获取键 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
输出应该是一个数组,其中包含keys = object_id
的所有值。请指教?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我猜 Dereleased 的解决方案会更快(因为它使用内部循环),我的解决方案还可以处理
object_id
键的数组值。您的权衡;)如您所见,结果数组将作为参考提供给函数的每次调用(由
&
表示)。这样,该函数的每次递归调用都可以访问相同数组,并且只需添加一个查找即可。你可以这样做:
它为我提供了你的数组:
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 ;)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:
It gives me for your array:
我的测试必须简单,因为我不会花时间将您提供的任何转储转换为工作数组;如果这不起作用,请提供数组的
var_export
用于测试目的。然而,这听起来像是
array_walk_recursive()< 的工作/代码>
。
array_walk_recursive 的最后一个参数是数组中的引用,因为调用时传递引用已被弃用,这是唯一的方法(
除了一些复杂的对象结构 参见下文)将对容器的引用传递给处理函数。此代码应使用所有以“object_id”作为键的值填充
$coll
。它不适用于“object_id”是数组的情况,因为这是array_walk_recursive()
的已知行为(即,不会为本身就是数组的元素启动回调函数)。编辑(为了正义):
对象结构实际上并不那么复杂,所以如果您希望避免无关的数组/引用构造,请执行以下操作:
工作原理
PHP 的
array_walk_recursive()
函数采用给定数组作为其第一个参数并迭代每个键/值对。如果值是标量,它将键/值组合传递给某个用户定义的回调函数;但是,如果该值是一个数组,则array_walk_recursive()
将递归调用自身,并继续以相同的方式运行。它将以这种方式工作,直到循环遍历数组中的每个元素。第二个参数是要使用的回调函数;在本例中,我编写并声明了一个函数 (
array_get_keys
),并将其名称作为第二个参数中的字符串传递。因此,该函数由array_walk_recursive()
是每次处理键/值对时执行的函数。这两个示例中的第三个参数基本上是一段“额外”数据,我们可以传递给回调函数以改变其行为——在本例中,它是一个数组(或者在第二个示例中是一个 ArrayObject 容器),它是( a) 在全局范围内定义,(b) 用于存储
$key == 'object_id'
为 true 的所有值。在第一个示例中,我使用语法
array(&$coll)
来传递集合变量,因为这是传递对集合的引用以便可以在全局范围(否则,我们无法检索列表)。在第二个示例中,我将$coll
声明为ArrayObject
,这使得它通过引用隐式传递,因此无需将其包含在数组中以确保它是在全局范围内修改。My testing had to be simple because I am not going to take the time to translate whatever dump you have provided into a working array; if this doesn't work, please provide a
var_export
of the array for testing purposes.This, however, sounds like a job for
array_walk_recursive()
.The last parameter to
array_walk_recursive
is a reference in an array because, since call-time pass-by-reference has been deprecated, that is the only way (other than some convoluted object structureSee Below) to pass a reference to the container to the handler function.This code should populate
$coll
with all the values that have 'object_id' as their key. It will not work for cases where 'object_id' is an array, because this is known behavior ofarray_walk_recursive()
(that is, the callback function is not initiated for elements which are themselves arrays).EDIT (for great justice):
The object structure is not actually that convoluted, so if you wish to avoid extraneous array/reference constructs, here's how to do it:
How it works
PHP's
array_walk_recursive()
function takes a given array for its first parameter and iterates over each key/value pair. If the value is scalar, it passes the key/value combination to some user-defined callback function; however, if the value is an array, thenarray_walk_recursive()
will recursively call itself, and continue to behave in the same manner. It will work in this way until it has looped through every element in the array.The second parameter is the callback function to use; in this case, I have written and declared a function (
array_get_keys
) and passed its name as a string in the second parameter. Thus, the function is registered byarray_walk_recursive()
and is the function executed each time a key/value pair is processed.The third parameter in both examples is basically a piece of "extra" data we can pass to our callback function to alter its behavior -- in this case, it is an array (or in the second example, an ArrayObject container) which is (a) defined in the global scope and (b) used to store all the values for which
$key == 'object_id'
is true.In the first example, I use the syntax
array(&$coll)
to pass the collection variable, because that is the only way to pass a reference to the collection so that it can be modified in the global scope (otherwise, we have no way to retrieve the list). In the second example, I declare$coll
as anArrayObject
, which makes it implicitly passed by reference, so there is no need to contain it in an array to ensure it is modified in the global scope.使用 Spl 迭代器时,在
foreach
循环中检查object_id
的替代方法:扩展 FilterIterator 的子类必须实现
accept()
方法。在内部,当使用foreach
迭代数组时,过滤器迭代器将调用您的accept
方法并跳过该方法中编写的测试将返回 false 的任何元素。 FilterIterators 是可堆叠的,因此您可以将多个 FilterIterators 组合到一个 Filter Chain 中,这使得这是一种非常灵活的方法。您可以像这样使用它
有关 SplIterators 的更多信息:
Alternative to checking for
object_id
inforeach
loop when using Spl Iterators:Subclasses extending FilterIterator must implement the
accept()
method. Internally, when iterating over your array withforeach
, the filter iterator will call youraccept
method and skip any elements for which the test written in the method will return false. FilterIterators are stackable, so you can combine multiple FilterIterators to a Filter Chain, which makes this a very flexible approach.You'd use it like this
More on SplIterators:
基于@goat的回答,我写了一个小函数:
false
:
使用而不管重复项:
使用排除重复项:
Based on @goat answer, I wrote a little function which :
false
if emptyfunction :
Use regardless of duplicates :
Use excluding duplicates :