PayPal重复历史记录转换为特定的数组格式

时间:2018-03-26 14:01:48

标签: php arrays paypal

我从PayPal定期发布的个人资料中获取了以下历史记录。请帮我转换成数组。我需要验证" P_TRANSTATE"价值到每个月。通过以下数组检查真的很难。请建议我或帮我将数组转换为以下视图。

[status] => 1
[result] => Array
    (
        [HTTP/1_1_200_OK ... RESULT] => 0
        [RPREF] => RGX51B669592
        [PROFILEID] => 0
        [P_PNREF1] => BL0PEE6F2E98
        [P_TRANSTIME1] => 25-Aug-17  04:46 AM
        [P_RESULT1] => 0
        [P_TENDER1] => C
        [P_AMT1] => 19.99
        [P_TRANSTATE1] => 8
        [P_PNREF2] => BP0PECB1799B
        [P_TRANSTIME2] => 24-Sep-17  04:58 AM
        [P_RESULT2] => 0
        [P_TENDER2] => C
        [P_AMT2] => 19.99
        [P_TRANSTATE2] => 8            
    );

需要遵循以下格式

1] => Array
    (
        [P_PNREF] => BL0PEE6F2E98
        [P_TRANSTIME] => 25-Aug-17  04:46 AM
        [P_RESULT] => 0
        [P_TENDER] => C
        [P_AMT] => 19.99
        [P_TRANSTATE] => 8


[2] => Array
    (
        [P_PNREF] => BP0PECB1799B
        [P_TRANSTIME] => 24-Sep-17  04:58 AM
        [P_RESULT] => 0
        [P_TENDER] => C
        [P_AMT] => 19.99
        [P_TRANSTATE] => 8  );

2 个答案:

答案 0 :(得分:2)

如果我没有误解你的问题那么这对你有用。

<?php
$array =  array
    (
    'HTTP/1_1_200_OK ... RESULT' => 0,
    'RPREF' => 'RGX51B669592',
    'PROFILEID' => 'RP0000000040',
    'P_PNREF1' => 'BQ1PECD4AEB8',
    'P_TRANSTIME1' => '25-Aug-17  04:46 AM',
    'P_RESULT1' => 0,
    'P_TENDER1' => 'C',
    'P_AMT1' => 19.99,
    'P_TRANSTATE1' => 8,
    'P_PNREF2' => 'BT1PFFF8A110',
    'P_TRANSTIME2' => '24-Sep-17  04:58 AM',
    'P_RESULT2' => 0,
    'P_TENDER2' => 'C',
    'P_AMT2' => 19.99,
    'P_TRANSTATE2' => 8,            
);

unset($array['HTTP/1_1_200_OK ... RESULT'],$array['RPREF'],$array['PROFILEID']);
$final_array = [];
foreach($array as $key=>$value){
    $index = substr($key, -1);
    $key = substr($key, 0, -1);    
    $final_array[$index][$key] = $value; 
}
print '<pre>';
print_r($final_array);
print '</pre>';
?>

<强>输出:

Array
(
    [1] => Array
        (
            [P_PNREF] => BQ1PECD4AEB8
            [P_TRANSTIME] => 25-Aug-17  04:46 AM
            [P_RESULT] => 0
            [P_TENDER] => C
            [P_AMT] => 19.99
            [P_TRANSTATE] => 8
        )

    [2] => Array
        (
            [P_PNREF] => BT1PFFF8A110
            [P_TRANSTIME] => 24-Sep-17  04:58 AM
            [P_RESULT] => 0
            [P_TENDER] => C
            [P_AMT] => 19.99
            [P_TRANSTATE] => 8
        )

)

DEMO: https://eval.in/978871

根据 Parapluie 的评论:您可以使用preg_match()在9之后越过数字时获取键/索引,因为substr($key,-1)或者substr($key,0,-1)将无法正常工作。见下文 -

$re = '/(\D+)(\d+)/';
foreach($array as $key=>$value){
    preg_match($re, $str, $matches);
    $index = $matches[2];
    $key = $matches[1];  
    $final_array[$index][$key] = $value; 
}

答案 1 :(得分:0)

你可以这样做,假设$ res是paypal数组响应。

<?php
  function convertArray($arr){
  $temp = array();
  $temp[1] = $arr['result'];
  return $temp;
  }
  print_r(convertArray(r$es));
?>