Array count for each value in turn syntax? [message #169864] |
Tue, 28 September 2010 23:41 |
GarryJones
Messages: 21 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
An array contains the values 109,110,117
The table contains
ItemTypeCode - ItemData
109 njj
109 jfdjf
109 mdmd
109 jdjd
110 jf
110 jfjf
117 jgjg
117 jfjfjf
118 nfnf
I want to use the array to give me the results of how many there are
of each type
So the output would be
109 = 4
110 = 2
117 = 2
(in this case the entries for type 118 are not displayed because 118
it not in the array)
The table data is just an example, in my actual table its a few more
columns.
I plan to show these in a table with bold row headings with
statistical data before the actual data rows
The row headings will need to contain the number of data rows in the
following data rows belonging to its type.
I can run through the array and use a counter, then output the number
then run through the array again to display the data. I am guessing I
don't have to do that, surely these is some kind of "how many are" php
command? But I am stuck here.
Any help greatly appreciated.
Garry Jones
Sweden
|
|
|
Re: Array count for each value in turn syntax? [message #169866 is a reply to message #169864] |
Tue, 28 September 2010 23:44 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/28/2010 7:41 PM, GarryJones wrote:
> An array contains the values 109,110,117
>
> The table contains
>
> ItemTypeCode - ItemData
>
> 109 njj
> 109 jfdjf
> 109 mdmd
> 109 jdjd
> 110 jf
> 110 jfjf
> 117 jgjg
> 117 jfjfjf
> 118 nfnf
>
> I want to use the array to give me the results of how many there are
> of each type
>
> So the output would be
> 109 = 4
> 110 = 2
> 117 = 2
> (in this case the entries for type 118 are not displayed because 118
> it not in the array)
>
> The table data is just an example, in my actual table its a few more
> columns.
>
> I plan to show these in a table with bold row headings with
> statistical data before the actual data rows
>
> The row headings will need to contain the number of data rows in the
> following data rows belonging to its type.
>
> I can run through the array and use a counter, then output the number
> then run through the array again to display the data. I am guessing I
> don't have to do that, surely these is some kind of "how many are" php
> command? But I am stuck here.
>
> Any help greatly appreciated.
>
> Garry Jones
> Sweden
Sounds like you need to learn to use a SQL database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Array count for each value in turn syntax? [message #169867 is a reply to message #169864] |
Wed, 29 September 2010 00:22 |
Hamish Campbell
Messages: 15 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
On Sep 29, 12:41 pm, GarryJones <mor...@algonet.se> wrote:
> An array contains the values 109,110,117
>
> The table contains
>
> ItemTypeCode - ItemData
>
> 109 njj
> 109 jfdjf
> 109 mdmd
> 109 jdjd
> 110 jf
> 110 jfjf
> 117 jgjg
> 117 jfjfjf
> 118 nfnf
>
> I want to use the array to give me the results of how many there are
> of each type
>
> So the output would be
> 109 = 4
> 110 = 2
> 117 = 2
> (in this case the entries for type 118 are not displayed because 118
> it not in the array)
>
> The table data is just an example, in my actual table its a few more
> columns.
>
> I plan to show these in a table with bold row headings with
> statistical data before the actual data rows
>
> The row headings will need to contain the number of data rows in the
> following data rows belonging to its type.
>
> I can run through the array and use a counter, then output the number
> then run through the array again to display the data. I am guessing I
> don't have to do that, surely these is some kind of "how many are" php
> command? But I am stuck here.
>
> Any help greatly appreciated.
>
> Garry Jones
> Sweden
The MySQL manual has an example that should be instructive:
<http://dev.mysql.com/doc/refman/5.0/en/group-by-
functions.html#function_count>
If you want to count the number of array items with the same value, it
would be very inefficient to iterate through the array for each item.
One simple way would look like:
<?php
$items = array(
"apple", "banana", "apple", "pear", "orange", "pear", "apple"
);
$totals = array();
// Count totals for each $items key:
foreach($items as $item)
isset($totals[$item]) ? $totals[$item] = 1 : $totals[$item]++;
print_r($totals);
// result: Array ( [apple] => 3 [banana] => 1 [pear] => 2 [orange] =>
1 )
|
|
|
Re: Array count for each value in turn syntax? [message #169887 is a reply to message #169864] |
Wed, 29 September 2010 12:14 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 29 Sep, 00:41, GarryJones <mor...@algonet.se> wrote:
> An array contains the values 109,110,117
>
> The table contains
>
> ItemTypeCode - ItemData
>
> 109 njj
> 109 jfdjf
> 109 mdmd
> 109 jdjd
> 110 jf
> 110 jfjf
> 117 jgjg
> 117 jfjfjf
> 118 nfnf
>
> I want to use the array to give me the results of how many there are
> of each type
>
> So the output would be
> 109 = 4
> 110 = 2
> 117 = 2
> (in this case the entries for type 118 are not displayed because 118
> it not in the array)
>
> The table data is just an example, in my actual table its a few more
> columns.
>
> I plan to show these in a table with bold row headings with
> statistical data before the actual data rows
>
> The row headings will need to contain the number of data rows in the
> following data rows belonging to its type.
>
> I can run through the array and use a counter, then output the number
> then run through the array again to display the data. I am guessing I
> don't have to do that, surely these is some kind of "how many are" php
> command? But I am stuck here.
>
> Any help greatly appreciated.
>
> Garry Jones
> Sweden
If this data is in a table as you say it is, you should be using SQL
functions to do this not php ones.
|
|
|
Re: Array count for each value in turn syntax? [message #169969 is a reply to message #169867] |
Sat, 02 October 2010 10:53 |
August Karlstrom
Messages: 16 Registered: October 2010
Karma: 0
|
Junior Member |
|
|
On 2010-09-29 02:22, Hamish Campbell wrote:
> isset($totals[$item]) ? $totals[$item] = 1 : $totals[$item]++;
If $totals[$item] has been set you want to increment its value, not set
it to one, so it should be the other way around. The statement is also
better written as
$totals[$item] = isset($totals[$item]) ? $totals[$item] + 1 : 1;
In your version there are side effects inside the conditional expression
and you ignore the result of it which is considered somewhat bad
programming style.
/August
--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
|
|
|