Re: Count the Number of Elements Using Explode

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

 



2008/10/31 Stut <stuttle@xxxxxxxxx>:
> 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
>>
>> 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

For the case where you want to explode a string and afterwards count
the number of elements you wind up with, Maciek is right: count() is
the correct function to use. array_count_values() will not work on the
original string and after exploding will have no
useful effect on the resulting string as far as counting the number of
separators goes.

If you don't actually need to explode the string but just want to
count the separators, you could use some weird logic to get
array_count_values() to count the number of times '|' appeared in the
original string--but it would be pointless. count_chars() would be a
much better choice for what you are suggesting.


Torben

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