Page 1 of 1

splitting and combining from 2 variables

Posted: Sun Jan 11, 2004 8:21 am
by d52477001
Hi,
i have a slight problem as follows:

I have two variables with comma delimited values in them. I want to combine the first value of each variable, and the second, etc. I do not know how many value there will be in each variable (although they will have the same amount).

example:
$name = "joe watson,mark denby,maria smooker";
$favoritecolour = "blue,red,green";

I then want to get an output of:
joe watson: blue
mark denby: red
maria smooker: green

I have tried using strtok() and explode() but as yet i have had no success.
Does anyone have any ideas on how to solve this?
Help is much appreciated.

Djurre Broekhuis.

Posted: Sun Jan 11, 2004 8:27 am
by Bennettman

Code: Select all

<?php

list($name,$favoritecolour) = array(explode(",",$name),explode(",",$favoritecolour));
for($i=0; $name[$i]; $i++) {
   print $name[$i] . ": " . $favoritecolour[$i] . "<br />";
} 

?>

Posted: Sun Jan 11, 2004 8:37 am
by d52477001
i seem to get a parse error in the line:

list($name,$favoritecolour) = array(explode(","$name),explode(","$favoritecolour));
?>

Posted: Sun Jan 11, 2004 8:51 am
by Gen-ik
Might be easier to understand if you are new to PHP...

Code: Select all

<?php

$names = explode(",", $name); // Gives you an array of names
$colours = explode(",", $colours); // Gives you an array of colours

for($i=0; $i<count($names); $i++)
echo '{$names[$i]} : {$colours[$i]}<br />';

?>

Posted: Sun Jan 11, 2004 8:52 am
by d52477001
there were some comma missing in that line, it should have been:

list($name, $favoritecolour) = array(explode(",", $name) , explode(",", $favoritecolour));

So i don't get the parse error anymore instead i get:

Warning: Illegal offset type in /home2/jyscompu/public_html/sbup/d52477001/php/debug/extest1.php on line 8
:

Warning: Illegal offset type in /home2/jyscompu/public_html/sbup/d52477001/php/debug/extest1.php on line 8
:

Warning: Illegal offset type in /home2/jyscompu/public_html/sbup/d52477001/php/debug/extest1.php on line 8
:


My code is as follows:

Code: Select all

<?php

$name = "joe watson,mark denby,maria smooker"; 
$favoritecolour = "blue,red,green"; 

list($name, $favoritecolour) = array(explode(",", $name) , explode(",", $favoritecolour));
foreach($name as $key -> $tempname) { 
echo $tempname . ": " . $favoritecolour[$key] . "<br />"; 
}


?>

Posted: Sun Jan 11, 2004 8:59 am
by d52477001
Gen-ik:

Thanks! that works (almost) flawlessly.

that should be double quotes like this:
echo "{$names[$i]} : {$colours[$i]}<br />";

Thanks very much!

Djurre Broekhuis

Posted: Sun Jan 11, 2004 9:11 am
by Gen-ik
d52477001 wrote:Gen-ik:

Thanks! that works (almost) flawlessly.

that should be double quotes like this:
echo "{$names[$i]} : {$colours[$i]}<br />";

Thanks very much!

Djurre Broekhuis
No problem... and it can be either single or double quotes. As you start to use PHP for more complicated things you will probably find yourself using single quotes quite a lot... it makes it easier when you are doing something like echo '<form method="post" action="bla.php">'

Posted: Sun Jan 11, 2004 9:58 am
by Bennettman
Yeah, just after I posted the original code in my post I tried it out and it didn't work, so switched it with what's there now.

Posted: Sun Jan 11, 2004 10:45 am
by vigge89
Gen-ik wrote: No problem... and it can be either single or double quotes. As you start to use PHP for more complicated things you will probably find yourself using single quotes quite a lot... it makes it easier when you are doing something like echo '<form method="post" action="bla.php">'
well, i'm using echo "<form method='post' action='bla.php'>";, and that works well :P