2016-01-15 81 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

但我 關於使用類似的原因很可疑這在問題中給出。我建議你進一步調查「問題」,並做而不是使用這個。