簡體   English   中英

在多維數組中查找特定鍵的值

[英]Find value of specific key in multidimensional array

從json提要中,我收到一個多維數組,如下所示。 在這個數組中,我需要特定鍵的一些值,例如[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
            )

    )

)

從純粹的技術角度來看,您可以執行以下操作

<?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;
}

看到http://docs.php.net/RecursiveIteratorIteratorhttp://docs.php.net/RecursiveArrayIterator

但我 寧願 關於使用像這樣的問題,給出的理由很可疑。 我建議你查“問題”進一步不要用這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM