我正在使用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等)的值,这些值有几层。
我不是在要求任何人为我编写代码,但是如果有人可以解释我如何遍历此方法以达到我所需要的...
提前非常感谢。
当你的数组结构具有许多嵌套级别(可能会更改)时,可以使用递归函数来过滤所需的项目:
在此示例中,我们循环浏览$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中引入)
工作演示
那绝对是有史以来最好的答案。非常感谢!您不仅为我提供了帮助,而且还可以将您所做的功能添加到我的库中,以备将来使用!
我在获取另外两个值时遇到问题...在此示例中,我需要的是“ fee_amount” ['value'],它是-10.88,在此示例中,是“ tax_amount” [''value'],它是15.71。当我尝试将“费用金额”和/或“ tax_amount”添加到针数组中,然后像对item_unit_price那样进行调用时,出现此错误:注意:C:\ xampp \ htdocs \ retail \ paypal中的未定义偏移量:0 .php在第62行,即“ $ result [$ needle = find($ json,$ needle)[0];”。回显时,在foreach循环中的一行...以及“注意:尝试访问类型为null的值的数组偏移量”。有什么不同??
另外,当我在$ needles数组中添加'line2'时,如果该特定订单没有第二条地址行,则会引发错误...在有两条地址行的订单中没有错误。如果没有结果,添加的东西不会破坏代码...但是我也不知道该怎么做... = /
嘿@Tim B,更新了答案。希望对您有所帮助。如果您有后续问题,建议您发布一个包含详细信息的新问题。小心!
非常感谢您向我介绍了空合并运算符?? ...我现在将尝试所有操作