2012-11-08 24 views
1

我正在調用PayPals API TransactionSearch,並且獲取平坦的數據數組。我需要重建這個數組。下面是我回來從貝寶結構:我需要陣列只是以下復位PHP重建數組Paypal TransactionSearch函數

array(36){ 
[ 
    "L_TIMESTAMP0" 
]=>string(28)"2012%2d09%2d18T22%3a10%3a13Z"[ 
    "L_TIMESTAMP1" 
]=>string(28)"2012%2d09%2d18T19%3a55%3a41Z"[ 
    "L_TIMESTAMP2" 
]=>string(28)"2012%2d09%2d18T19%3a55%3a41Z"[ 
    "L_TIMEZONE0" 
]=>string(3)"GMT"[ 
    "L_TIMEZONE1" 
]=>string(3)"GMT"[ 
    "L_TIMEZONE2" 
]=>string(3)"GMT"[ 
    "L_TYPE0" 
]=>string(7)"Payment"[ 
    "L_TYPE1" 
]=>string(7)"Payment"[ 
    "L_TYPE2" 
]=>string(7)"Payment"[ 
    "L_EMAIL0" 
]=>string(26)"XXXXX%40hotmail%2ecom"[ 
    "L_EMAIL1" 
]=>string(31)"XXXX%40lvcoxmail%2ecom"[ 
    "L_EMAIL2" 
]=>string(23)"XXXXt%2ecom"[ 
    "L_TRANSACTIONID0" 
]=>string(17)"13E586955G649992Y"[ 
    "L_TRANSACTIONID1" 
]=>string(17)"8LH96897T3119113R"[ 
    "L_TRANSACTIONID2" 
]=>string(17)"87U867057E085230E"[ 
    "L_STATUS0" 
]=>string(9)"Completed"[ 
    "L_STATUS1" 
]=>string(9)"Completed"[ 
    "L_STATUS2" 
]=>string(9)"Completed"[ 
    "L_AMT0" 
]=>string(7)"85%2e00"[ 
    "L_AMT1" 
]=>string(7)"85%2e00"[ 
    "L_AMT2" 
]=>string(7)"85%2e00"[ 
    "L_CURRENCYCODE0" 
]=>string(3)"USD"[ 
    "L_CURRENCYCODE1" 
]=>string(3)"USD"[ 
    "L_CURRENCYCODE2" 
]=>string(3)"USD"[ 
    "L_FEEAMT0" 
]=>string(9)"%2d2%2e17"[ 
    "L_FEEAMT1" 
]=>string(9)"%2d2%2e17"[ 
    "L_FEEAMT2" 
]=>string(9)"%2d2%2e17"[ 
    "L_NETAMT0" 
]=>string(7)"82%2e83"[ 
    "L_NETAMT1" 
]=>string(7)"82%2e83"[ 
    "L_NETAMT2" 
]=>string(7)"82%2e83"[ 
    "TIMESTAMP" 
]=>string(28)"2012%2d11%2d08T14%3a24%3a30Z"[ 
    "CORRELATIONID" 
]=>string(13)"52c22d68648cd"[ 
    "ACK" 
]=>string(7)"Success"[ 
    "VERSION" 
]=>string(6)"51%2e0"[ 
    "BUILD" 
]=>string(7)"4137385" 

}

: [ 「L_STATUSn」] =>串(9) 「已完成」 [「L_TRANSACTIONIDn」] =>字符串(17)「8LH96897T3119113R」

'n'是數字,是貝寶返回的數組鍵值。

這是我使用的代碼,它是borked。

 $i = 0; 
    $c = 0; 
    foreach ($comparison AS $aKey => $v) { 
     $findme1 = 'L_TIMESTAMP'.$i++; 
     $findme2 = 'L_STATUS'.$c++; 
     $txid = $myarray[$findme1]; 
     $status = $myarray[$findme2]; 
     $TXid = array_search('$findme1', $aKey); 
     $Status = array_search('$findme2', $aKey); 
     $TxID[] = array('Status' => $aStatus, 'TransactionID' => $aTransactionID); 
     } 

感謝更高級的方式來重建這個數組,我試圖使用的方法似乎不是太高效。

回答

1

稍遲,但也許其他人也有這個要求。所以在這裏:

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; 
} 

只是從你的curl調用的結果,並將結果作爲格式化數組。

注意註釋掉的行,有一些字段是全局的,比如版本,如果你想要這些,取消註釋,但是你可能不得不調整一些格式化代碼。

,如果你想這個送入PHPExcel對象,你可以這樣做,像這樣:停止了

$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++; 
    } 
+0

感謝,希望別人可以用這個 –