Re: Array help needed
| From: | php3 at developersdesk dot com | Date: | Thu, 18 Jan 2001 12:53:41 +0000 |
| Subject: | Re: Array help needed | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-35336@lists.php.net to get a copy of this message | ||
Addressed to: CDitty <mail@redhotsweeps.com>
php-general@lists.php.net
** Reply to note from CDitty <mail@redhotsweeps.com> Thu, 18 Jan 2001 05:40:13 -0600
>
> Hello all,
>
> I have 6 numbers that are in an array. I need to know how many times
> each number shows up in that array.
>
> ex.... Numbers: 1,3,5,1,4,1
>
> Output needed: (Not necessarily like this) 1 3 3 1 5 1 4 1
>
> Can anyone help me with this? I looked in the manual, but couldn't
> find anything that was quite what I needed. I have tried sorting
> then array_count_values, but ended up with a result like this. Some
> results have counts, some don't.
>
> 1 2 1 4 1 4 4 3 5 1
>
$Input = array( 1, 3, 5, 1, 4, 1 );
$Output = array();
while( list( $index, $value ) = each( $Input )) {
$Output[ $Value ] ++;
}
ksort( $Output );
reset( $Output )
while( list( $index, $value ) = each( $Output )) {
echo( 'There are ' . $value . ' - ' .
$index . "s in the array<BR>\n"
}
Not tested, but this should turn return:
There are 3 - 1s in the array
There are 1 - 3s in the array
There are 1 - 4s in the array
There are 1 - 5s in the array
Rick Widmer
Internet Marketing Specialists
http://www.developersdesk.com