0

我正在尝试获取PayPal帐户的交易详细信息,我成功地这样做了,但是我得到了这么长(我假设是一个数组),我需要将详细信息插入MySQL数据库。我尝试了几种foreach方法,但显然我做错了什么。

这是PayPal请求成功后的响应(存储为$httpParsedResponse

TransactionSearch Completed Successfully: Array (
    [L_TIMESTAMP0] => 2012%2d08%2d14T18%3a46%3a00Z 
    [L_TIMESTAMP1] => 2012%2d08%2d14T18%3a45%3a32Z 
    [L_TIMESTAMP2] => 2012%2d08%2d13T01%3a45%3a48Z 
    [L_TIMESTAMP3] => 2012%2d08%2d13T01%3a34%3a12Z 
    [L_TIMEZONE0] => GMT 
    [L_TIMEZONE1] => GMT 
    [L_TIMEZONE2] => GMT 
    [L_TIMEZONE3] => GMT 
    [L_TYPE0] => Payment 
    [L_TYPE1] => Payment 
    [L_TYPE2] => Payment 
    [L_TYPE3] => Transfer 
    [L_EMAIL0] => pmd_1333866_per%40yahoo%2ecom 
    [L_EMAIL1] => pmd_1333866_per%40yahoo%2ecom 
    [L_EMAIL2] => pmd_1333866_per%40yahoo%2ecom 
    [L_NAME0] => Joshua%20O 
    [L_NAME1] => Joshua%20O 
    [L_NAME2] => Joshua%20O 
    [L_NAME3] => PayPal 
    [L_TRANSACTIONID0] => 58289472YK615973G 
    [L_TRANSACTIONID1] => 4U113116FL1819900 
    [L_TRANSACTIONID2] => 2X978155KR373471P 
    [L_TRANSACTIONID3] => 3TH38971MF599302V 
    [L_STATUS0] => Completed 
    [L_STATUS1] => Completed 
    [L_STATUS2] => Completed 
    [L_STATUS3] => Completed 
    [L_AMT0] => %2d100%2e00 
    [L_AMT1] => %2d50%2e00 
    [L_AMT2] => %2d200%2e00 
    [L_AMT3] => 2000%2e00 
    [L_CURRENCYCODE0] => USD 
    [L_CURRENCYCODE1] => USD 
    [L_CURRENCYCODE2] => USD 
    [L_CURRENCYCODE3] => USD 
    [L_FEEAMT0] => 0%2e00 
    [L_FEEAMT1] => 0%2e00 
    [L_FEEAMT2] => 0%2e00 
    [L_FEEAMT3] => 0%2e00 
    [L_NETAMT0] => %2d100%2e00 
    [L_NETAMT1] => %2d50%2e00 
    [L_NETAMT2] => %2d200%2e00 
    [L_NETAMT3] => 2000%2e00 
    [TIMESTAMP] => 2012%2d08%2d25T17%3a59%3a01Z 
    [CORRELATIONID] => bf5b1a824d937 
    [ACK] => Success 
    [VERSION] => 51%2e0 
    [BUILD] => 3435050
)

显然,我猜该数组的结构为

 [L_TIMESTAMP0]
 [L_TIMESTAMP1]
 [L_TIMESTAMP2]

并且对于每组有多少组交易细节,即时间戳,时区,类型,电子邮件等......

在这种情况下,有 3 组交易细节,但无论是只有 1 笔交易还是 100 笔交易,任何解决方案都需要能够遍历它。

就像我说的,这里唯一的目标是从 PayPal 获取这个返回的数组并插入 MySQL 数据库。

我希望这是有道理的,任何帮助都将不胜感激!谢谢!

对不起,我不得不编辑我的旧问题,因为我猜我是新手 - 无论如何,我认为你的代码有助于更好地组织它,这是我现在得到的回应......仍然对如何将其插入 mysql 感到困惑数据库...

Array ( [0] => Array ( [timestamp] => 2012%2d08%2d14T18%3a46%3a00Z 
[timezone] => GMT 
[type] => Payment 
[email] => pmd_1333866_per%40yahoo%2ecom 
[name] => Joshua%20O 
[transactionid] => 58289472YK615973G 
[status] => Completed 
[amt] => %2d100%2e00 
[feeamt] => 0%2e00 
[netamt] => %2d100%2e00 )

[1] => Array ( [timestamp] => 2012%2d08%2d14T18%3a45%3a32Z 
[timezone] => GMT 
[type] => Payment 
[email] => pmd_1333866_per%40yahoo%2ecom 
[name] => Joshua%20O 
[transactionid] => 4U113116FL1819900
[status] => Completed
[amt] => %2d50%2e00 
[feeamt] => 0%2e00 
[netamt] => %2d50%2e00 ) 

[2] => Array ( [timestamp] => 2012%2d08%2d13T01%3a45%3a48Z 
[timezone] => GMT 
[type] => Payment 
[email] => pmd_1333866_per%40yahoo%2ecom 
[name] => Joshua%20O 
[transactionid] => 2X978155KR373471P 
[status] => Completed 
[amt] => %2d200%2e00 
[feeamt] => 0%2e00 
[netamt] => %2d200%2e00 ) 

[3] => Array ( [timestamp] => 2012%2d08%2d13T01%3a34%3a12Z 
[timezone] => GMT 
[type] => Transfer 
[email] => 
[name] => PayPal 
[transactionid] => 3TH38971MF599302V 
[status] => Completed 
[amt] => 2000%2e00 
[feeamt] => 0%2e00 
[netamt] => 2000%2e00 ) )
4

1 回答 1

0

两个想法是:

$numOfTransactions=intval((count($response)-5)/11);

其中 5 是每个事务不重复的值的数量,11 是每个事务的值的数量。这取决于结构是否可靠。

或者

$numOfTransactions=0;
foreach ($response as $key=>$value){
 $check=strpos($key,'L_TIMESTAMP');
 if ($check===false) break;
 $numOfTransactions++;
}

然后您可以遍历响应以构建数据

for ($i=0;$i<$numOfTransactions;$i++){
 $data[$i]=array("timestamp"=>$response['L_TIMESTAMP'.$i],"timezone"=>$response['L_TIMEZONE'.$i]...);
}
于 2012-08-26T00:17:38.533 回答