Jump to content

Splitting data into arrays


mme

Recommended Posts

Hi,

 

I am trying to split a string into an array and then seeing what sort of data it is.

 

eg;

 

I get the following string

 

api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse

 

I need to be able to put this into an array so in this case

 

myarray[type]=somethingelse
myarray[api]=somerandomkeyhere
myarray[user]=someuser
myarray[password]=somehashvaluehere

 

but it could include many more/less different value's separated by the & symbol.

 

Thanks,

 

-mme

Link to comment
https://forums.phpfreaks.com/topic/236548-splitting-data-into-arrays/
Share on other sites

$strings=explode("&","string here")

 

Gets you:

 

$strings array("api=somerandomkey", "user=someuser", "ect.")

 

 

foreach($strings as $string){
$string=explode("=",$string);
}

 

$string array("api","somerandomkey", "user", "someuser", "ect.")

 

$string[0]=$string[1];

$string[0] could also be $string["api"]

Use the function parse_str

 

<?php
$str = 'api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse';
parse_str($str,$ary);
print_r($ary);
?>

 

Ken

 

Thanks I did not know such a simple function existed. This was exactly what I was looking for.

 

-mme

Use the function parse_str

 

<?php
$str = 'api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse';
parse_str($str,$ary);
print_r($ary);
?>

 

Ken

 

Thanks I did not know such a simple function existed. This was exactly what I was looking for.

 

-mme

 

Neither did I. Much simpler than my method.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.