Page 1 of 1

explode - find the number of parts

Posted: Sun Oct 11, 2009 3:00 pm
by JKM
I'm having a mysql field that could be like:
<----------
1,2
1

3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}

Re: explode - find the number of parts

Posted: Sun Oct 11, 2009 3:03 pm
by markusn00b
JKM wrote:I'm having a mysql field that could be like:
<----------
1,2
1

3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}
What is your question, JKM? Pardon my ignorance.

explode() returns an array of elements that were split according to the delimiter. Just like with any other array, you can find how many elements one has using the count() function.

Hope this helps,
Mark.

Re: explode - find the number of parts

Posted: Sun Oct 11, 2009 3:43 pm
by JKM
Sorry for not writing my question in the main text element (I wrote it in the subject).

Lets say that count($explode); returns 3, how can I list the three results?

Re: explode - find the number of parts

Posted: Sun Oct 11, 2009 4:02 pm
by markusn00b
JKM wrote:Sorry for not writing my question in the main text element (I wrote it in the subject).

Lets say that count($explode); returns 3, how can I list the three results?
You generally loop over the array.

Code: Select all

 
$str = "my, delimited, text";
$arr_str = explode(", ", $str);
foreach ($arr_str as $key => $node) {
    printf("%d = %s\n", $key, $node);
}
 
Something like that.

For debugging purposes, you might use the print_r() function to display an array's contents.

Other resources: Hope this helps,
Mark.