Check how many times specific value in array PHP
Several ways.
$cnt = count(array_filter($uid,function($a) {return $a==12;}));
or
$tmp = array_count_values($uid);
$cnt = $tmp[12];
or any number of other methods.
Count how many times a value appears in this array?
array_column
will create an array of all the elements in a specific column of a 2-D array, and array_count_values
will count the repetitions of each value in an array.
$newArr = array_count_values(array_column($ids, 'id'));
Count How Many Times a Value Appears Php
array_count_values
, enjoy :-)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
//basically we get the same array
foreach ($array as $data) {
$count[] = $data;
}
$vals = array_count_values($count);
print_r($vals);
Result:
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)
PHP - count specific array values
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
Count how often a particular value appears in an array
PHP has a function called array_count_values
for that.
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
How do I count occurrence of duplicate items in array
array_count_values
, enjoy :-)
$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);
Result:
No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)
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 get how many times a specific value change in php array
Here in an example-
$count = 0;
$counter = -1;
$arr = array("3", "6", "6", "3", "6", "4", "4");
foreach($arr as $val){
if($val == 6)
$counter = 0;
if($counter == 0 && $val != 6){
$count++;
$counter = -1;
}
}
echo $count;
Count number of values in array with a given value
How about using array_count _values to get an array with everything counted for you?
Related Topics
PHP Regex Get Text Between Bbcode Tags
Is There a Difference Between Instantiation with Parentheses or Without
How to Use Date() in Doctrine 2 Dql
Insert Value List Does Not Match Column List: 1136 Column Count Doesn't Match Value Count
Php: What If I Call a Static Method in Non-Static Way
Interpreting Return Value of Function Directly as an Array
Mysql: Determine Table's Primary Key Dynamically
Send Email by Email Class in Codeigniter with Gmail
Strtolower() for Unicode/Multibyte Strings
How to Set the Value of a Textbox Through PHP
Add a Shipping to an Order Programmatically in Woocommerce 3
How to Ensure I Caught All Errors from MySQLi::Multi_Query
Adding Additional Persist Calls to Preupdate Call in Symfony 2.1
Get the Selected Index Value of <Select> Tag in PHP