PHP - Count Specific Array Values

PHP - count specific array values

$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];

How to count specific value from array PHP?

Try this. See comments for step-by-step explanation.
Outputs:

array(2) {
["channel_1"]=>
int(1)
["channel_0"]=>
int(2)
}

Code:

<?php

// Your input array.
$a =
[
[
'Report' =>
[
'id' => 10,
'channel' => 1
]
],
[
'Report' =>
[
'id' => 92,
'channel' => 0
]
],

[
'Report' =>
[
'id' => 18,
'channel' => 0
]
]
];

// Output array will hold channel_N => count pairs
$result = [];

// Loop over all reports
foreach ($a as $report => $values)
{
// Key takes form of channel_ + channel number
$key = "channel_{$values['Report']['channel']}";

if (!isset($result[$key]))
// New? Count 1 item to start.
$result[$key] = 1;
else
// Already seen this, add one to counter.
$result[$key]++;
}

var_dump($result);
/*
Output:
array(2) {
["channel_1"]=>
int(1)
["channel_0"]=>
int(2)
}
*/

PHP - count specific array values, multiple values

// change your code to this
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Mary","Ben");
$counts = array_count_values($array);
$one = $counts['Ben']; // 3
$two = $counts['Phil']; // 1
$three = $counts['Mary']; // 2
echo $one + $two + $three; // 6

// because of this :)
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number <-- your case
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
$bar = &$foo; // This is a valid assignment.
$bar = &(24 * 7); // Invalid; references an unnamed expression.

More on PHP variables

Count specific values in multidimensional array

$number_of_green_fruit = 0;
for ($row = 0; $row < 3; $row++) {
if($fruit[$row]["color"]=="green") {
$number_of_green_fruit++;
echo $fruit[$row]["name"] . '<br />';
}
}

Count specific array values with same part

array_reduce() will do the job

$count = array_reduce(array_keys($foo), function($c, $k){
return preg_match('/^gr\d+$/', $k) ? ++$c : $c;
}, 0);

How to count value inside array?

You could make use of a combination of explode, array_map, array_filter and array_count_values to split the values on a comma and keep the ones for which the values are 100.

For example:

$items = [
1 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
2 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
3 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
];

foreach ($items as &$item) {
$item = array_map(function ($x) {
$res = array_filter(array_count_values(
explode(',', $x)
), function ($key) {
return $key === 100;
}, ARRAY_FILTER_USE_KEY);
return sprintf("100 (%s) Times", count($res) === 1 ? $res[100] : 0);
}, $item);
}

print_r($items);

Result

Array
(
[1] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

[2] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

[3] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

)

Php demo

How to count specific array values and assign the values in a table data

To count the number of employees for each position, you can use array_count_values on the position column of the the employees array. For example:

$emp_positions = array_count_values(array_column($employees, 'position'));
foreach ($positions as $key => $value) {
echo $positions[$key] . ' : ' . $emp_positions[$key] . ' employees' . PHP_EOL;
}

Output:

TL : 1 employees 
Programmer : 2 employees
Converter : 2 employees

Demo on 3v4l.org



Related Topics



Leave a reply



Submit