I have loaded the results of an SQL query into an array. The fields are ComputerName
, time_in_days
, room
, keyed on ComputerName
.
I would like to find out how many times each time_in_days
occurrence happens.
Example values of array are:
[STU-CZC1087SNC] => Array
(
[ComputerName] => STU-CZC1087SNC
[time_in_days] => 0
[room] => 4Q08
)
[STU-CZC02501QT] => Array
(
[ComputerName] => STU-CZC02501QT
[time_in_days] => 12
[room] => 2R017
)
So I want to know how many computers have time_in_days = 12
, and how many have time_in_days = 0
for example.
It will be used to plot a graph.
How do I do / what is the best way of doing this?
$result = count(
array_filter(
$myArray,
function($value) {
return $value['time_in_days'] == 12;
}
)
);
Try this, where $input is your array of arrays with the computer data:
$result = array();
foreach($input as $computerName => $arr){
if(!isset($result[ $arr['time_in_days'] ]))
$result[ $arr['time_in_days'] ] = 0;
$result[ $arr['time_in_days'] ]++;
}
return $result;
The result will be something like:
[
12 => 2, //2 computers have time_in_days of 12
0 => 1 //1 computer has time_in_days of 0
]
OK, here's the working example:
<?php
$computerarray = [
'STU-CZC1087SNC' => Array
(
'ComputerName' => 'STU-CZC1087SNC',
'time_in_days' => 0,
'room' => '4Q08'
),
'STU-CZC02501QT' => Array
(
'ComputerName' => 'STU-CZC02501QT',
'time_in_days' => 12,
'room' => '2R017'
),
'STU-CZC02501QF' => Array
(
'ComputerName' => 'STU-CZC02501QT',
'time_in_days' => 12,
'room' => '2R017'
)
];
$myarray = array();
foreach($computerarray as $key => $val){
$myarray[] = $val['time_in_days'];
}
echo "<pre>";
$myarray = array_count_values($myarray);
print_r($myarray);
This should work -
$my_arr = Array();
array_walk($arr, function($v) use(&$my_arr) {$my_arr[] = $v['time_in_days'];});
$time_freq = array_count_values($my_arr);
var_dump($time_freq);
/**
OUTPUT
**/
array
0 => int 1
12 => int 2
I'll remove my comment on the question that says:
The best way to do this is to not use php, but sql to
GROUP BY
time_in_days
and callCOUNT()
. Here's a push in the right direction .
As for a conditional-less, functional approach, here is a one-liner using the two functions that were specifically designed for this task: array_count_values() and array_column()
Code: ( Demo )
$computerarray = [
'STU-CZC1087SNC' => ['ComputerName' => 'STU-CZC1087SNC','time_in_days' => 0,'room' => '4Q08'],
'STU-CZC02501QT' => ['ComputerName' => 'STU-CZC02501QT','time_in_days' => 12,'room' => '2R017'],
'STU-CZC02501QF' => ['ComputerName' => 'STU-CZC02501QT','time_in_days' => 12,'room' => '2R017']
];
var_export(array_count_values(array_column($computerarray,'time_in_days')));
Output:
array (
0 => 1,
12 => 2,
)
也许array_count_values,请参见手册: http ://pl1.php.net/array_count_values
foreach($ya as $a) {
$newarr[$a['time_in_days']]++;
}
$total = null;
foreach ($members as $owner):
if($owner['circle_id'] == $c['id']): // or if($owner['circle_id'] == 1):
$total = $total + 1;
endif;
endforeach;
echo $total;
This code calculates the number of members within a circle. It matches the members Table id with the current circle id. Its current value is $c['id'] = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.