splitting and combining from 2 variables
Moderator: General Moderators
splitting and combining from 2 variables
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.
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
Code: Select all
<?php
list($name,$favoritecolour) = array(explode(",",$name),explode(",",$favoritecolour));
for($i=0; $name[$i]; $i++) {
print $name[$i] . ": " . $favoritecolour[$i] . "<br />";
}
?>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 />';
?>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:
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 />";
}
?>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">'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
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
well, i'm using echo "<form method='post' action='bla.php'>";, and that works wellGen-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">'