Array ( [TIMESTAMP] => 2012%2d05%2d01T17%3a19%3a07Z [CORRELATIONID] => 3aeb192ad6388 [ACK] => Failure [VERSION] => 51%2e0 [BUILD] => 2764190 [L_ERRORCODE0] => 10527 [L_SHORTMESSAGE0] => Invalid%20Data [L_LONGMESSAGE0] => This%20transaction%20cannot%20be%20processed%2e%20Please%20enter%20a%20valid%20credit%20card%20number%20and%20type%2e [L_SEVERITYCODE0] => Error [AMT] => 10%2e00 [CURRENCYCODE] => GBP )

Hello everyone,

Can you please suggest on how to parse responses? It is the form of an array but I would like to know where I can create a function to get the specfic parse like collecting the l_longmessage<number>.

Thanks in advance. I hope you can give me some thoughts.

Member Avatar for diafol

You have what appears to be url encoded strings (%20 = space).

So:

//assume the array you supplied is called $the_array
$decoded_array = array_map('rawurldecode',$the_array);
$output = "";
foreach($decoded_array as $key=>$value){
    if(is_int(substr($key, -1)){
       $key = substr($key,0,-1) . " #" . substr($key, -1); //this displays the integer apart from the main keyname - although only for single digits - more sophisticated stuff can be done with things like preg functions
    }
    $output .= $key ." = ". $value . "<br />";

}

THis ain't pretty, but may give an idea

After urldecode you can use extract function, which will create variable based on array variable.

<?
    $myArray['TIMESTAMP'] = '2012%2d05%2d01T17%3a19%3a07Z';
    $myArray['L_LONGMESSAGE0'] = 'This%20transaction%20cannot%20be%20processed';
    $myArray['CURRENCYCODE'] = 'Euro';
    $myArrayFinal = array_map('rawurldecode',$myArray);

    extract($myArrayFinal);

    echo 'Time: '.$TIMESTAMP;
    echo 'Long Message: '.$L_LONGMESSAGE0;
    echo 'Currency: '.$CURRENCYCODE;
?>

This%20transaction%20cannot%20be%20processed

Member Avatar for diafol

I thought he needed the integer from the array key, i.e. 0 from L_LONGMESSAGE0

Thanks for the replies. I will give it a try.

Member Avatar for diafol

With regard to extract, from the php.net manual:

Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

Should be OK, but take care.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.