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.

Recommended Answers

All 2 Replies

No need to write such lengthy code.
Check parse_url function Click Here

Member Avatar for diafol

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.

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.