This is a discussion on Separate text within the PHP General forums, part of the PHP Programming Forums category; Lo, I have a variable $groups. I can contain a single number like $groups=1. But It also can contain ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Lo,
I have a variable $groups. I can contain a single number like $groups=1. But It also can contain more numbers like $groups=1,4,3. I need to separate the numbers and put them in a kind off array. Something like: $groups[1] = 1 $groups[2] = 4 $groups[3] = 3 Anybody an idea ? Bert |
|
|||
|
Bertman wrote:
> Lo, > > I have a variable $groups. I can contain a single number like $groups=1. But > It also can contain more numbers like $groups=1,4,3. I need to separate the > numbers and put them in a kind off array. > Something like: > $groups[1] = 1 > $groups[2] = 4 > $groups[3] = 3 > > Anybody an idea ? I wouldn't make a exception if there's only one number, just always use an array. $groups = array(1); // first example $groups = array(1, 4, 3); // second example But I don't really get your problem? |
|
|||
|
Bertman wrote: > Lo, > > I have a variable $groups. I can contain a single number like $groups=1. But > It also can contain more numbers like $groups=1,4,3. I need to separate the > numbers and put them in a kind off array. > Something like: > $groups[1] = 1 > $groups[2] = 4 > $groups[3] = 3 Use the explode() function <http://www.php.net/explode> <? $groups ='1,4,3'; $groups_ary = explode(',',$groups); print_r($groups); ?> Ken |