Re: [PHP] Count the Number of Elements Using Explode



On 31 Oct 2008, at 17:32, Maciek Sokolewicz wrote:
Kyle Terry wrote:
---------- Forwarded message ----------
From: Kyle Terry <kyle@xxxxxxxxxxxxx>
Date: Fri, Oct 31, 2008 at 9:31 AM
Subject: Re: [PHP] Count the Number of Elements Using Explode
To: Alice Wei <ajwei@xxxxxxxxxxxxx>
I would use http://us2.php.net/manual/en/function.array-count-values.php
On Fri, Oct 31, 2008 at 9:29 AM, Alice Wei <ajwei@xxxxxxxxxxxxx> wrote:
Hi,

I have a code snippet here as follows:

$message="1|2|3|4|5";
$stringChunks = explode("|", $message);

Is it possible to find out the number of elements in this string? It seems
that I could do things like print_r and find out what is the last element of
$stringChunks is, but is there a way where I can use code from the two lines
and have it print out 5 as the number of elements that contains after the
splitting?

Or, am I on the wrong direction here?

Thanks in advance.

Alice
_________________________________________________________________
Check the weather nationwide with MSN Search: Try it now!
http://search.msn.com/results.aspx?q=weather&FORM=WLMTAG
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



First of all, don't top post, please.
Secondly, that won't do what the OP asked for. array_count_values returns an array which tells you how often each VALUE was found in that array. so: array_count_values(array(1,2,3,4,5)) will return: array(1=>1,2=>1,3=>1,4=>1,5=>1). While what the OP wanted was: some_function(array(1,2,3,4,5)) to return 5 (as in the amount of values, not how often each value was found in the array).
count() would do it directly, and the other suggestions people gave do it indirectly, assuming that the values between '|' are never > 1 char wide.

I think you'll find Kyle was suggesting that the OP use that function to count the |'s in the string. Add 1 to that and you have the number of elements explode will produce. More efficient if you're simply exploding it to count the elements, but count would be better if you need to explode them too.

-Stut

--
http://stut.net/
.



Relevant Pages

  • Re: arrays -- can they be words, not bytes?
    ... of pixels, each 16-bits deep, is run-length-encoded into a smaller C array ... ASCII string so that I can send it via HTTP POST command to a PHP script. ... I have an array of unsigned shorts here". ...
    (comp.lang.php)
  • Re: threadsort
    ... >script to php. ... >Both scripts now produce the same output. ... >first string in each array of string. ... the array in the following code is sorted how I think you want ...
    (comp.lang.php)
  • Re: $request problem
    ... $array['key'] is an array with the string 'key' as index. ... from OUTSIDE a doublequote'd string, ... The PHP manual says on this topic: ... That would explain why you get no warnings! ...
    (comp.lang.php)
  • Re: arrays -- can they be words, not bytes?
    ... am I accessing string elements? ... string there, or an array? ... ASCII string so that I can send it via HTTP POST command to a PHP script. ... documentation on type array is likely to mislead you. ...
    (comp.lang.php)
  • Re: alternative php syntax
    ... I am building an array of strings in the form of "number=", ... I literally mean to assign string values such as: ... know php is loose about declaring variables. ...
    (alt.php)