Re: Count the Number of Elements Using Explode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:  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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux