• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

split string into name value pairs

i have a string of unknown length.

id/2/somename/name/something/else

I need to split it into name value pairs.
id=2
somename=name
something=else
0
phenixfilms
Asked:
phenixfilms
2 Solutions
 
BlomholmCommented:
Well, first you'd need to split the string on character ' / '.
then you'll get an array of even size (I assumed the string won't suddenly stop without giving value to a name. (like if it suddenly stopped on "id", without giving the "2" )
so you can loop through the array incrementing the index by 2 each time and put the name value pairs into another array.

something like this:

$string = "id/2/somename/name/something/else";
$string = explode('/', $string);
 
$nameValues = array();
 
for($i = 1; i < count($string); $i = $i+2) {
   $nameValues[$string[0]] = $string[1]; 
}
 
//now you could access the values like $nameValues["somename"] and it should contain "name";

Open in new window

0
 
BlomholmCommented:
made a typo in previous post. inside forloop should be:
$nameValues[$string[$i]] = $string[$i+1];

Open in new window

0
 
BlomholmCommented:
bah, another. So need an edit button here =p

whole example corrected:

$string = "id/2/somename/name/something/else";
$string = explode('/', $string);
 
$nameValues = array();
 
for($i = 0; i < count($string); $i = $i+2) {
   $nameValues[$string[0]] = $string[1]; 
}
 
//now you could access the values like $nameValues["somename"] and it should contain "name";

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BlomholmCommented:
=)
$string = "id/2/somename/name/something/else";
$string = explode('/', $string);
 
$nameValues = array();
 
for($i = 0; i < count($string); $i = $i+2) {
   $nameValues[$string[$i]] = $string[$i+1]; 
}
 
//now you could access the values like $nameValues["somename"] and it should contain "name";

Open in new window

0
 
phenixfilmsAuthor Commented:
infinite loop on string with trailing slash :)
0
 
ddrudikCommented:

<pre>
<?php
$sourcestring="id/2/somename/name/something/else";
preg_match_all('~([^/]+)/([^/]*)~',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
// if you want to make additional assignments:
for ($i = 0; $i < count($matches[0]); $i++) {
	${$matches[1][$i]}=$matches[2][$i];
}
echo "<hr>\$something is equal to '$something'.";
// or:
for ($i = 0; $i < count($matches[0]); $i++) {
	$result[$matches[1][$i]]=$matches[2][$i];
}
echo "<hr>".print_r($result,true);
?>

Open in new window

0
 
caterham_wwwCommented:
What about cutting the '/' if present and - to avoid errors - check for a a even number
$string = 'id/2/somename/name/something/else';
if(substr($string, -1, 1) == '/')
{
    $string = substr($string, 0, -1);
}
 
$string = explode('/', $string);
$num = count($string);
 
if($num%2 == 0)
{
    $nameValues = array();
    for($i = 0; $i < $num; ++$i)
    {
        $nameValues[$string[$i]] = $string[++$i]; 
    }
}

Open in new window

0
 
agamalCommented:
Try this .....
<?php
$string = 'id/2/somename/name/something/else';
$string = explode('/', $string);
for ($i=0;$i< count($string);$i++) {
$j= $i+1;
echo $string[$i]."=".$string[$j]."<br>";
$i++;
}
 ?>

Open in new window

0
 
Imme-emosolCommented:
@caterham_www:
$string = 'id/2/somename/name/something/else';
if(substr($string, -1, 1) == '/')
{
    $string = substr($string, 0, -1);
}
#Is probably better written as:
$string = trim('id/2/somename/name/something/else','/');

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now