PHP - iterate through array and only show certain values -
i trying show selection of transactions using php , paypal's nvp transactionsearch
method.
i transactions appear cannot tidy response , show specific values after - in case being 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 $key => $value){ echo $key.' => '.$value."<br />"; }
which outputs in 1 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 , on...
how can instead output this: (so goes through each transaction next rather each 'value' , onto next, plus not show 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
i think array of arrays better option:
foreach($result $k => $v) { preg_match('~([a-z_]+)(\d+)$~', $k, $m); $nested[$m[2]][$m[1]] = $v; }
which creates nested array 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 easy generate desired output this.
Comments
Post a Comment