简体   繁体   中英

Find value of specific key in multidimensional array

From a json feed I receive a multidimensional array like below. From this array I need some values from specific keys, like the value at [payment_url]. I'm aware I can just get the value with

 $arr['transactions'][0]['payment_url'];

But since the documentation is a bit 'concise' I'm not sure if there could be a response with an extra level.

Henche, I would like a function which I feed with the key name and returns the value unregarded on which level the key is. Something like

getValue($arr, 'payment_url');

How do I do this?

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
            )

    )

)

From a purely technical perspective you could do something like

<?php
$response = json_decode(data(), true);
$flat = flatten($response);

echo $flat['payment_url'], ' | ', $flat['issuer_id'];

function flatten(array $arr) {
    $rii = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::LEAVES_ONLY );
    foreach( $rii as $k=>$v) {
        $rv[$k] = $v;
    }
    return $rv;
}


function data() {
    return <<< eoj
[
  {
    "id": "1dc05e5f-c455-4f06-bc9f-37a2db3a75e1",
    "created": "2014-07-14T10:13:50.726519+00:00",
    "modified": "2014-07-14T10:13:51.593830+00:00",
    "merchant_order_id": "EXAMPLE001",
    "status": "new",
    "type": "payment",
    "amount": 995,
    "currency": "EUR",
    "description": "Example order #1",
    "return_url": "http://www.example.com/",
    "transactions": [
      {
        "id": "90b70bba-e298-4687-a2f2-095f7ebc9392",
        "created": "2014-07-14T10:13:51.082946+00:00",
        "modified": "2014-07-14T10:13:51.210838+00:00",
        "status": "new",
        "currency": "EUR",
        "amount": 995,
        "description": "Example order #1",
        "expiration_period": "P30D",
        "balance": "internal",
        "payment_method": "ideal",
        "payment_method_details": {
          "issuer_id": "INGBNL2A"
        },
        "payment_url": "https://api.gingerpayments.com/redirect/90b70bba-e298-4687-a2f2-095f7ebc9392/to/payment/"
      }
    ]
  }
]
eoj;
}

see http://docs.php.net/RecursiveIteratorIterator , http://docs.php.net/RecursiveArrayIterator

But I'm rather very suspicious about the reason for using something like this given in the question. I suggest you investigate the "problem" further and do not use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM