0

I am trying to show a selection of transactions using PHP and Paypal's NVP TransactionSearch method.

I successfully get the transactions to appear but i cannot tidy the response up and show only the specific values i am after - in this case being just the following 4 values per transaction:

L_TIMESTAMP, 
L_TYPE, 
L_TRANSACTIONID

My current code is:

$request = http_build_query(array(
    "METHOD"    => $action,
    "VERSION"   => $config['version'],
    "USER"      => $config['username'],
    "PWD"       => $config['password'],
    "SIGNATURE" => $config['signature'],
    "STARTDATE" => $start,
    "EMAIL" => $email,
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);    
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

$result = curl_exec($ch);

if (!$result) {
    echo 'no results';
}

parse_str($result, $result);

foreach($result as $key => $value){
    echo $key.' => '.$value."<br />";
}

which outputs everything in one as below :

L_TIMESTAMP0 => 2014-01-15T11:29:21Z
L_TIMESTAMP1 => 2014-01-15T11:09:05Z
L_TIMEZONE0 => GMT
L_TIMEZONE1 => GMT
L_TYPE0 => Refund
L_TYPE1 => Payment
L_TRANSACTIONID0 => 7MY22222222222914
L_TRANSACTIONID1 => 2045555555555594D

and so on...

How can i instead output it like this: (so it goes through each transaction then the next rather than each 'value' and onto the next, plus not show the TIMEZONE key)

L_TIMESTAMP0 => 2014-01-15T11:29:21Z
L_TYPE0 => Refund
L_TRANSACTIONID0 => 7MY22222222222914

L_TIMESTAMP1 => 2014-01-15T11:09:05Z
L_TYPE1 => Payment
L_TRANSACTIONID1 => 2045555555555594D
4
  • what does $result look like before you run parse_str Commented Aug 18, 2014 at 13:46
  • You have to use array_filter() Commented Aug 18, 2014 at 13:46
  • if i simply remove the parse_str from the above code i get nothing outputted on the page Commented Aug 18, 2014 at 13:47
  • Asking two questions in one is not very effective to get good answers. "How to show only the values from wanted keys ?" has tons of duplicate on SO Commented Aug 18, 2014 at 13:51

2 Answers 2

3

I think an array of arrays will be a better option:

foreach($result as $k => $v) {
    preg_match('~([A-Z_]+)(\d+)$~', $k, $m);
    $nested[$m[2]][$m[1]] = $v;
}

which creates a nested array like this:

Array
(
    [0] => Array
        (
            [L_TIMESTAMP] => 2014-01-15T11:29:21Z
            [L_TIMEZONE] => GMT
            [L_TYPE] => Refund
            [L_TRANSACTIONID] => 7MY22222222222914
        )

    [1] => Array
        (
            [L_TIMESTAMP] => 2014-01-15T11:09:05Z
            [L_TIMEZONE] => GMT
            [L_TYPE] => Payment
            [L_TRANSACTIONID] => 2045555555555594D
        )

)

It should be easy to generate your desired output from this.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_filter inside wrapping loop. Like this:

for ($i = 0; ; $i++) {
        $arr = array_filter($result, function ($v) use ($i) {
                return substr($v, -1) == $i;
        });
        if (!$arr) break;
        foreach($arr as $key => $value){
            echo $key.' => '.$value."<br />";
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.