Warm tip: This article is reproduced from serverfault.com, please click

其他-从paypal api存储复杂json返回数组的php变量

(其他 - store php variables of complex json return array from paypal api)

发布于 2020-11-30 02:26:19

我正在使用PayPal API向我提供有关特定交易的信息。我正在使用PHP和curl,并且一切正常。PayPal返回的结果是一个JSON对象。

这是在$ JSON = json_decode($ result,true)之后,当我将其发送到print_r($ JSON)时,JSON对象给出的输出示例。

Array ( [transaction_details] => Array ( [0] => Array ( [transaction_info] => Array ( [paypal_account_id] => U*****8W [transaction_id] => 0EC********3404V [transaction_event_code] => T0006 [transaction_initiation_date] => 2020-11-24T04:22:32+0000 [transaction_updated_date] => 2020-11-24T04:22:32+0000 [transaction_amount] => Array ( [currency_code] => USD [value] => 364.71 ) [fee_amount] => Array ( [currency_code] => USD [value] => -10.88 ) [transaction_status] => S [transaction_subject] => Payment to Seller [custom_field] => seller_e*******1-4d4c-90d1-51e0eae77f2c_1 [protection_eligibility] => 01 ) [payer_info] => Array ( [account_id] => UWM*****28W [email_address] => s******@networkcowboy.org [address_status] => Y [payer_status] => Y [payer_name] => Array ( [given_name] => Scott [surname] => D******e [alternate_full_name] => S*************e ) [country_code] => US ) [shipping_info] => Array ( [name] => S*t Do**le [address] => Array ( [line1] => 8** St [city] => La**od [state] => CO [country_code] => US [postal_code] => 80215 ) ) [cart_info] => Array ( [item_details] => Array ( [0] => Array ( [item_code] => LUMI23398 [item_name] => Samsung Galaxy S10e (T-Mobile) [SM-G970U] - Black, 128 GB, 6 GB [item_quantity] => 1 [item_unit_price] => Array ( [currency_code] => USD [value] => 349.00 ) [item_amount] => Array ( [currency_code] => USD [value] => 349.00 ) [tax_amounts] => Array ( [0] => Array ( [tax_amount] => Array ( [currency_code] => USD [value] => 15.71 ) ) ) [total_item_amount] => Array ( [currency_code] => USD [value] => 364.71 ) ) ) ) [store_info] => Array ( ) [auction_info] => Array ( ) [incentive_info] => Array ( ) ) ) [account_number] => PR6L***PXQN [start_date] => 2020-11-01T07:00:00+0000 [end_date] => 2020-11-29T21:59:59+0000 [last_refreshed_datetime] => 2020-11-29T21:59:59+0000 [page] => 1 [total_items] => 1 [total_pages] => 1 [links] => Array ( [0] => Array ( [href] => https://api.paypal.com/v1/reporting/transactions?end_date=2020-11-29T23%3A59%3A59-0700&transaction_id=0EC78****43404V&fields=all&start_date=2020-11-01T00%3A00%3A00-0700&page_size=100&page=1 [rel] => self [method] => GET ) ) )

如你所见,JSON对象极其复杂……至少对我而言。我需要提取一些值并将其保存到变量中。

我需要提取transaction_initiation_date,item_code,alternate_full_name,line1,city,state,postal_code,email_address和其他超级复杂的东西(例如price等)的值,这些值有几层。

我不是在要求任何人为我编写代码,但是如果有人可以解释我如何遍历此方法以达到我所需要的...

提前非常感谢。

Questioner
Tim B
Viewed
11
jibsteroos 2020-12-01 06:39:13

当你的数组结构具有许多嵌套级别(可能会更改)时,可以使用递归函数来过滤所需的项目:

在此示例中,我们循环浏览$needles要从源数组中检索的项目的列表(数组)。在内部,foreach()我们调用递归函数find(),如果找到,则返回result(match)。所有结果都存储在array中$result当然,如果只需要搜索一项,则可find()以为该项调用

<?php
$needles = ['transaction_initiation_date', 'item_code', 'alternate_full_name', 'line1', 'city', 'state', 'postal_code', 'email_address', 'item_unit_price'];

foreach($needles as $needle) {
    $result[$needle] = find($arr, $needle)[0];
}

function find(array $arr, string $needle, array &$result = []) {
    foreach($arr as $key => $value) {
        if($key !== 'item_unit_price' && is_array($value)) {
            find($value, $needle, $result);
        } else {
            if($key === $needle) {
                $result[] = $value;
                break;
            }
        }
    }
    return $result;
}

使用提供的数据,你将获得一些选定项目的以下输出:

echo 'item code : ' . $result['item_code'];
echo '<br />';
echo 'city : ' . $result['city'];
echo '<br />';
echo 'price : ' . $result['item_unit_price']['value'];
echo '<br />';

item code : LUMI23398
city : La**od
price : 349.00

工作演示


编辑

请注意源数组中将数组作为值保存的项目,例如“ fee_amount”或“ tax_amount”。如果你将其中任何一个都放$needles进去

这就是我们做出“例外”的原因:

if($key !== 'item_unit_price' && is_array($value)) {

“ item_unit_price”也包含一个数组。

因此,如果要过滤包含数组的任何键,则可以创建它们的列表,并将条件更改为:

if (!in_array($key, ['item_unit_price',
                         'fee_amount', 
                         'tax_amount'], true) && is_array($value)) {

至于$needles源数组中可能不存在的数组中项目的问题(例如“ line2”),请更改:

$result[$needle] = find($arr, $needle)[0];

$result[$needle] = find($arr, $needle)[0] ?? 'n/a';

空合并运算符 ??,如果它存在,并且不为NULL返回它的第一个操作数,否则返回第二个操作数。(在PHP7中引入)

工作演示