0

I am trying to process an array to get variable name and its value. below is the code.

$s = explode("&",$mine);
foreach($s as $k=>$v){
 $v=explode("=",$v);
for($i=0;$i<count($v);$i++){
  $a[$v[$i]]=$v[$i+1];
}
print_r($a);
}

The prolem is that the array repeats itself in output. as shown(look at it keenly):

Array ( [APIresult] => 000;APIresultexplnation [000;APIresultexplnation] => Transaction Created;TransactionToken [Transaction Created;TransactionToken] => 79A9DC2A-622B-4334-B610-001F4976C6C0 [79A9DC2A-622B-4334-B610-001F4976C6C0] => )

I want it give variable and value e.g [APIresult] => 000;TransactionToken => 79A9DC2A-622B-4334-B610-001F4976C6C0. The response i.e original string is this:

APIresult=000;APIresultexplnation=Transaction Created;TransactionToken=D2AB45E4-1DE7-4828-96CA-3A884697109A

This is a response from a third party API. How can i get round this because i want to get the result for each variable for easy processing.

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by diafol
0

The response i.e original string is this:

APIresult=000;APIresultexplnation=Transaction Created;TransactionToken=D2AB45E4-1DE7-4828-96CA-3A884697109A

I'm sure there's a more concise way of putting it, but this seems to work for the string you mentioned:

$str = "APIresult=000;APIresultexplnation=Transaction Created;TransactionToken=D2AB45E4-1DE7-4828-96CA-3A884697109A";
$r = explode(";",$str);
foreach($r as $i){
    $bits = explode("=",$i);
    $array[$bits[0]] = $bits[1];        
}
print_r($array);

You could roll this up inside a function or class method if you needed to.

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.