Splitting up a value

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
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Splitting up a value

Post by VKX »

I have a value, $shows, that contains a list of dates like this:

2005-01-20,2005-03-05,2006-04-01

There are an undeterminded number of values in $shows. How can I split them up and up them in variables? IE,

$date1 = 2005-01-20
$date2 = 2005-03-05
$date3 = 2006-04-01

Thanks in advance guys!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Great! So my code now looks like this:

Code: Select all

<?php
		$p_show = explode(",", $p_tbmshows);
			echo $p_show[0]; echo "<br>";
			echo $p_show[1]; echo "<br>";
		?>
How could I loop is so that it echo's as many times as there are rows in the array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

With a loop. It could be suggested that you need to read the manual a bit more...

while, for, foreach
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Use a for loop or a foreach loop...

Code: Select all

<?php
$p_show = explode(",", $p_tbmshows);

foreach ($p_show as $show_this)
{
    echo $show_this . "<br />\n";
}

// Or alternatively
for ($i = 0; $i < count($p_show); $i++)
{
    echo $p_show[$i] . "<br />\n";
}
?>
EDIT: Dang, Feyd the speedtastic beat me to it. I thought I was typing fast, too. Oh well, just goes to show that Feyd can literally post 3000 different replies in like four seconds. (And he knows what he talking about, too).
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

feyd wrote:With a loop. It could be suggested that you need to read the manual a bit more...

while, for, foreach
Of course I gave that a shot, but it's giving me an infinite loop. I don't know array stuff very well, as I'm self taught and still learning. I figured I had to do something special for the array . . .
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Just syntax errors. It's nested in a bunch of code.

Thanks again!
Post Reply