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
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"]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.