开发者

Format PayPal array nicely in PHP

开发者 https://www.devze.com 2023-01-15 00:47 出处:网络
Ihave an array returning like this from PayPal: Array ( [responseEnvelope.timestamp] => 2010-09-07T23%3A06%3A33.737-07%3A00

I have an array returning like this from PayPal:

Array
(
    [responseEnvelope.timestamp] => 2010-09-07T23%3A06%3A33.737-07%3A00
    [responseEnvelope.ack] => Success
    [responseEnvelope.correlationId] => b9d9ca4f54f44
    [responseEnvelope.build] => 1437064
    [estimatedAmountTable.currencyConversionList(0).baseAmount.code] => USD
    [estimatedAmountTable.currencyConversionList(0).baseAmount.amount] => 1.00
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(0).code] => EUR
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(0).amount] => 0.71
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(1).code] => USD
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(1).amount] => 1.00
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(2).code] => GBP
    [estimatedAmountTable.currencyConversionList(0).currencyList.currency(2).amount] => 0.48
)

The function that does this is:

function _deformat($nvpstr) {
    foreach (explode('&', $nvpstr) as $nvp) {
        list($key, $value) = explode('=', $nvp);
        $data[$key] = $value;
    }
    return $data;
}

I want the array like this:

..
[EUR]
   [amount] => 0.71
[GBP]
   [amount] => 0开发者_StackOverflow.48
...

I can get the key:

$new_key = substr(strrchr($key, "."), 1);

but I'm in a flat spin after that.


you can run on the array , and change the keys and the values like :

foreach ($myarray as $key=>$val) {

    if(strstr($key,'code')){
      $myarray[$val] = $myarray[str_replace('code','amount',$key)];
      unset($myarray[$key]);
      unset($myarray[str_replace('code','amount',$key)]);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消