Page 1 of 1

spliting/exploding help please!

Posted: Sat Nov 02, 2002 8:18 am
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 :)

Posted: Sat Nov 02, 2002 9:06 am
by volka
$arr = explode(',', $var);
or did I miss something? ;)

http://www.php.net/manual/en/function.explode.php

Posted: Sat Nov 02, 2002 9:08 am
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?

Posted: Sat Nov 02, 2002 9:11 am
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/>';

Posted: Sat Nov 02, 2002 12:09 pm
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

Posted: Sat Nov 02, 2002 3:57 pm
by volka
what is $product?

Posted: Sat Nov 02, 2002 4:38 pm
by DynamiteHost
$product would be one of the numbers in $var

Posted: Sat Nov 02, 2002 5:49 pm
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