douningqiu4991 2012-08-22 16:27 采纳率: 0%
浏览 27
已采纳

使用PHP数组的Paypal NVP TransactionSearch

Hello I am performing a query with the paypal API and my question concerns an array and retrieving values for the key (which is dynamic) I know the query works and credentials are good, as the following array is returned:

array(2) {
["L_TRANSACTIONID0"]=>
string(17) "9FX81733DJ079610B"
["L_TRANSACTIONID1"]=>
string(17) "5E083945JC6368706"
["L_TRANSACTIONID2"]=>
string(17) "7SP75180Y9281954W"
}

I am wanting to break each key out in a foreach loop, but each key is dynamic (notice the count on each key). How can this be accomplished in a foreach loop? Thanks,

  • 写回答

1条回答 默认 最新

  • dongshi3061 2013-05-25 13:57
    关注

    I just answered a similar question the other day, so potentially a duplicate. In any case, if this helps someone, here goes:

    function process_response($str)
    {
        $data = array();
        $x = explode("&", $str);
    
        foreach($x as $val)
        {
            $y = explode("=", $val);
    
            preg_match_all('/^([^\d]+)(\d+)/', $y[0], $match);
    
            if (isset($match[1][0]))
            {
                $text = $match[1][0];
                $num = $match[2][0];
    
                $data[$num][$text] = urldecode($y[1]);
            }
            else
            {
                $text = $y[0];
    //          $data[$text] = urldecode($y[1]);
            }
        }
    
        return $data;
    }
    

    Just feed the result from your curl call into this and take the result as a formatted array.

    Note the commented out line, there are some fields that are global, such as version, if you want these, uncomment, but then you may have to adjust some formatting code down stream.

    As an example of how to use this, say you want to feed this into PHPExcel object, you could do so like so:

        $response = your_curl_call($request);
        $data = process_response($response);
    
        $index = 1;
        foreach($data as $row)
        {
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$index, $row['L_TIMESTAMP'])
                ->setCellValue('B'.$index, $row['L_TIMEZONE'])
                ->setCellValue('C'.$index, $row['L_TYPE'])
                ->setCellValue('D'.$index, $row['L_EMAIL'])
                ->setCellValue('E'.$index, $row['L_NAME'])
                ->setCellValue('F'.$index, $row['L_TRANSACTIONID'])
                ->setCellValue('G'.$index, $row['L_STATUS'])
                ->setCellValue('H'.$index, $row['L_AMT'])
                ->setCellValue('I'.$index, $row['L_CURRENCYCODE'])
                ->setCellValue('J'.$index, $row['L_FEEAMT'])
                ->setCellValue('K'.$index, $row['L_NETAMT']);
            $index++;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起