0

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
GiANTOnFire
  • 193
  • 1
  • 4
  • 16

8 Answers8

2
$result = count(
    array_filter(
        $myArray,
        function($value) {
            return $value['time_in_days'] == 12;
        }
    )
);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

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
]
  • I would recommend an if/else because when your `if` is `true`, you write to `0` then immediately overwrite to `1`. More logical to use `if(...){...=1}else{++}` – mickmackusa Oct 20 '17 at 22:02
  • I really prefer it this way. It's in my opinion better readable/understandable code: always increment by one. And functionally separated: if its not set, then initialize the value. – Kapitein Witbaard Oct 23 '17 at 14:18
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);
hopsoo
  • 305
  • 1
  • 3
0

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
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
0

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 call COUNT(). 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,
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

Maybe array_count_values, see manual: http://pl1.php.net/array_count_values

hopsoo
  • 305
  • 1
  • 3
-1
foreach($ya as $a) {
    $newarr[$a['time_in_days']]++;
}
zogby
  • 462
  • 4
  • 15
-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;

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136