We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,055 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.

3
Contributors
2
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
mogaka
Junior Poster in Training
58 posts since Sep 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

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

vibhaJ
Posting Shark
958 posts since Apr 2010
Reputation Points: 161
Solved Threads: 190
Skill Endorsements: 3

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
Moderator
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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0671 seconds using 2.77MB