spliting/exploding help please!

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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

spliting/exploding help please!

Post by DynamiteHost »

Hi,

Say I had $var and it was holding "1,2,3,4,5,6" ....

How would I get it to split or explode the numbers?


Thanks :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$arr = explode(',', $var);
or did I miss something? ;)

http://www.php.net/manual/en/function.explode.php
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

how would i be able to use the variables which have been made?

would it be $arr[0], $arr[1] etc or what?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

exactly
e.g.

Code: Select all

for($i=0; $i!=count($arr); $i++)
   echo $arrї$i], '<br/>';

foreach ($arr as $value)
   echo $value, '<br/>';

foreach ($arr as $key=>$value)
   echo $key, '=>', $value, '<br/>';
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

hmm.. I don't think this is what I need... sorry :(

how would I be able to see if $var contained something equal to $product ?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what is $product?
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

$product would be one of the numbers in $var
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

$var = '1,2,3,4,5';
$product = 3;
$arr = explode(',', $var);
if (in_array($product, $arr)
{
    echo 'in array<br/>';
    $index = array_search($product, $var);
    echo $index;
}
http://www.php.net/manual/en/function.in-array.php
http://www.php.net/manual/en/function.array-search.php
Post Reply