Error in Array processing
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.
mogaka
Junior Poster in Training
58 posts since Sep 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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.
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
Question Answered as of 1 Year Ago by
vibhaJ
and
diafol