2016-01-15 53 views
1

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

    ) 

) 
+0

如果'transaction'数组中有多个事务会发生什么? – VolkerK

+0

这不会发生在我的代码。我甚至不确定是否有可能一次做更多的交易。文档没有提到任何相关内容。 –

+0

当你说“可能有额外的回应”时,我不确定你的意思。你可以给我们一些你认为它可能不是'$ arr ['transactions'] [0] ['payment_url']''的例子吗?这将帮助我们为您设计一个功能。 –

回答

0

从纯技术的角度来看,你可以

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

但我 关于使用类似的原因很可疑这在问题中给出。我建议你进一步调查“问题”,并做而不是使用这个。