splitting and combining from 2 variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d52477001
Forum Newbie
Posts: 6
Joined: Sun Jan 11, 2004 8:21 am
Location: Gaborone, Botswana

splitting and combining from 2 variables

Post 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.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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 />";
} 

?>
d52477001
Forum Newbie
Posts: 6
Joined: Sun Jan 11, 2004 8:21 am
Location: Gaborone, Botswana

Post by d52477001 »

i seem to get a parse error in the line:

list($name,$favoritecolour) = array(explode(","$name),explode(","$favoritecolour));
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 />';

?>
d52477001
Forum Newbie
Posts: 6
Joined: Sun Jan 11, 2004 8:21 am
Location: Gaborone, Botswana

Post 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 />"; 
}


?>
d52477001
Forum Newbie
Posts: 6
Joined: Sun Jan 11, 2004 8:21 am
Location: Gaborone, Botswana

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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">'
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
Post Reply