【问题标题】:Find value of specific key in multidimensional array在多维数组中查找特定键的值
【发布时间】:2016-01-15 21:49:20
【问题描述】:

从 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
            )

    )

)

【问题讨论】:

  • 如果transaction数组中有多个事务应该怎么办?
  • 这不会在我的代码中发生。我什至不确定是否可以一次进行更多交易。文档没有说明这一点。
  • 当您说“可能会有额外级别的响应”时,我不确定您的意思。你能给我们举例说明你认为它可能不是$arr['transactions'][0]['payment_url']吗?这将有助于我们为您设计一个功能。
  • gingerpayments.com/api#_orders 描述的API吗?如果是这样,您为什么不使用“官方” php 绑定:github.com/gingerpayments/ginger-php? (可能是有原因的,甚至是一个很好的原因,所以这确实是一个问题......)
  • 这也是一个有趣的解决方案:thereisamoduleforthat.com/content/dealing-deep-arrays-php - 但请听所有评论者说“你为什么要这样做”、“有更好的方法”和“使用姜官方 php绑定”

标签: php multidimensional-array


【解决方案1】:

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

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

但我相当非常怀疑使用问题中给出的类似内容的原因。我建议您进一步调查“问题”并不要使用它。

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多