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



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



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: need to send email to 1000 addresses (currently in MS Excel)- whats the best way to do that, cre
    ... > coding each line as an array then looping through the array with mail. ... >> don't post a question that doesn't have anything to remotely do with PHP, ... one huge string) and then it splits that string into an array of arrays. ... CC it, or even better, BCC it so that those being emailed can't ...
    (comp.lang.php)