我已将SQL查询的结果加载到数组中。字段为ComputerName
,time_in_days
,room
,键入ComputerName
。
我想了解每个time_in_days
发生的次数。
数组的示例值为:
[STU-CZC1087SNC] => Array
(
[ComputerName] => STU-CZC1087SNC
[time_in_days] => 0
[room] => 4Q08
)
[STU-CZC02501QT] => Array
(
[ComputerName] => STU-CZC02501QT
[time_in_days] => 12
[room] => 2R017
)
所以我想知道有多少台计算机有time_in_days = 12
,有多少计算机有time_in_days = 0
。
它将用于绘制图表。
我该怎么做/最好的方法是什么?
答案 0 :(得分:2)
$result = count(
array_filter(
$myArray,
function($value) {
return $value['time_in_days'] == 12;
}
)
);
答案 1 :(得分:1)
试试这个,其中$ input是包含计算机数据的数组数组:
$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;
结果如下:
[
12 => 2, //2 computers have time_in_days of 12
0 => 1 //1 computer has time_in_days of 0
]
答案 2 :(得分:0)
好的,这是工作示例:
<?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);
答案 3 :(得分:0)
这应该有效 -
$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
答案 4 :(得分:0)
我删除了对以下问题的评论:
执行此操作的最佳方法是不使用php,而是使用sql
month
GROUP BY
并调用time_in_days
。这是the right direction的推动。
对于无条件的功能方法,这里使用专门为此任务设计的两个函数:array_count_values()和array_column()
代码:(Demo)
COUNT()
输出:
$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')));
答案 5 :(得分:-1)
也许是array_count_values,参见手册: http://pl1.php.net/array_count_values
答案 6 :(得分:-1)
foreach($ya as $a) {
$newarr[$a['time_in_days']]++;
}
答案 7 :(得分:-1)
$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;
此代码计算圆圈内的成员数。它将成员Table id与当前圈号id匹配。其当前值为$c['id'] = 1