在多维数组中查找特定键的值

时间:2016-01-15 21:49:21

标签: php multidimensional-array

从json feed我收到一个如下所示的多维数组。从这个数组我需要一些来自特定键的值,比如[payment_url]的值。我知道我可以通过

获得价值
 $arr['transactions'][0]['payment_url'];

但是因为文档有点简洁'我不确定是否会有额外级别的回复。

Henche,我想要一个用键名称提供的函数,并在键的哪个级别返回未注册的值。像

这样的东西
getValue($arr, 'payment_url');

我该怎么做?

Array
(
[amount] => 2525
[client] => Array
    (
        [user_agent] => gingerphplib
    )

[created] => 2016-01-15T21:35:17.032535+00:00
[currency] => EUR
[description] => this is a description
[flags] => Array
    (
        [0] => is-test
    )

[id] => xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
[merchant_order_id] => 205
[modified] => 2016-01-15T21:35:17.734427+00:00
[project_id] => xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
[return_url] => http://path/to/return/url/test
[status] => new
[transactions] => Array
    (
        [0] => Array
            (
                [amount] => 2525
                [balance] => test
                [created] => 2016-01-15T21:35:17.231677+00:00
                [currency] => EUR
                [description] => dit is een omschrijving met een enter
                [id] => xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
                [modified] => 2016-01-15T21:35:17.612664+00:00
                [payment_method] => ideal
                [payment_method_details] => Array
                    (
                        [issuer_id] => INGBNL2A
                    )

                [payment_url] => https://link/to/payment/
                [status] => new
            )

    )

)

1 个答案:

答案 0 :(得分:0)

从纯粹的技术角度来看,可以做类似

的事情
{{1}}

请参阅http://docs.php.net/RecursiveIteratorIteratorhttp://docs.php.net/RecursiveArrayIterator

但我对而非非常怀疑在问题中使用这样的东西的原因。我建议你调查问题"进一步做使用它。