简体   繁体   English

PHP从数组中获取最高计数值

[英]PHP Get Highest Count Value from Array

In an array I have to find the object that is most recurring 在数组中,我必须找到最经常出现的对象

$items = array("370","370","546","55");
$value = max($items);
$key = array_search($value, $items);

This print out 2 and 546. 打印出2和546。

But how can I output 2 and its related value, so in my example 370 ? 但是如何输出2及其相关值,因此在示例370中呢?

I use array_count_values and from the resulting array use array_keys to get the numbers which appear most (eg 370) and array_values to get the number of times it appears (eg 2). 我使用array_count_values并从生成的数组中使用array_keys来获取出现次数最多的数字(例如370),并使用array_values来获取出现次数(例如2)。

<?php
$items   = [370,370,546,55];
$counted = array_count_values($items);
/*
Array
(
    [370] => 2
    [546] => 1
    [55] => 1
)

*/

$number_appears_most = array_keys($counted);
/*
Array
(
    [0] => 370
    [1] => 546
    [2] => 55
)
*/

$number_of_occurences = array_values($counted);
/*
Array
(
    [0] => 2
    [1] => 1
    [2] => 1
)
*/    
echo $number_appears_most[0] . ' appears ' . $number_of_occurences[0] . ' time(s)' . PHP_EOL;

This will give you: 这将为您提供:

370 appears 2 time(s)

Demo: https://eval.in/890626 演示: https : //eval.in/890626

I took @alistaircol Idea and not only fixed it (as it's dependent on the order of the array) but I also simplfied the assignment part so we are not creating 2 new arrays 我使用@alistaircol Idea不仅修复了它(因为它取决于数组的顺序),而且简化了赋值部分,所以我们不创建2个新数组

You can try it here 你可以在这里尝试

http://sandbox.onlinephpfunctions.com/code/2fe11fb6040dc68db43fb5cbb858ef3e47394dd2 http://sandbox.onlinephpfunctions.com/code/2fe11fb6040dc68db43fb5cbb858ef3e47394dd2

And here is the code 这是代码

$items   = [370,370,546,55,55,55];
$counted = array_count_values($items);

arsort($counted); //sort descending maintain keys

$occurences = reset($counted); //get the first value (rewinds internal pointer )
$most_frequent = key($counted); //get the key, as we are rewound it's the first key

echo $most_frequent . ' appears ' . $occurences . ' time(s)' . PHP_EOL;

This correctly outputs 正确输出

55 appears 3 time(s)

Just FYI @alistaircol original answer would fail this test case 仅供参考@alistaircol原始答案将无法通过此测试用例

http://sandbox.onlinephpfunctions.com/code/fe13e7a0bd00ea1219bc69e2bc5a5aebaf478034 http://sandbox.onlinephpfunctions.com/code/fe13e7a0bd00ea1219bc69e2bc5a5aebaf478034

Which is changing the input array from: 这将更改输入数组从:

  $items   = [370,370,546,55];

To: 至:

  $items   = [370,370,546,55,55,55];

It would still output: 它仍然会输出:

  370 appears 2 time(s)

In this case when the correct answer is 55 appears 3 time(s) 在这种情况下,正确答案是55 appears 3 time(s)

This way the item with the most is the last one, which exposes that it's based on the order ( which was obvious to me because of using the first index [0] ) 这样,最多的项目就是最后一个项目,这表明它是基于顺序的(这对我来说很明显,因为使用了第一个索引[0]

Not to call him out on it, but as it was accepted as the answer I felt I should point that out. 并不是要他大声疾呼,而是因为它被接受为答案,我觉得我应该指出这一点。 Overall though I was a sound approach to the question. 总的来说,尽管我是一个很好的方法。 So Kudos on that. 因此,在此赞誉。

UPDATE 更新

One way to get all values that show more then one time is like this: 一种获取所有显示一次以上的值的方法是这样的:

$items   = [370,370,546,55,55,55];

$unique = array_unique( $items );

$diff = array_diff_assoc( $items, $unique);

print_r( $diff );

Which outputs 哪个输出

Array
(
   [1] => 370
   [4] => 55
   [5] => 55
)

You can test it here http://sandbox.onlinephpfunctions.com/code/53df6a8ea7a68768572cfef494e3b715aa13e83b 您可以在这里进行测试http://sandbox.onlinephpfunctions.com/code/53df6a8ea7a68768572cfef494e3b715aa13e83b

One note, is that you will get exactly one less occurrence then is actually present. 需要注意的是,与实际出现的情况相比,您只会少发生一次。

UPDATE1 更新1

We can easily combine these and account for the missing one. 我们可以轻松地将它们结合起来并解决缺失的问题。 See this fiddle to test that 看到这个小提琴来测试

http://sandbox.onlinephpfunctions.com/code/1bc1a30a5e091af3a18fec2c8b48050869108549 http://sandbox.onlinephpfunctions.com/code/1bc1a30a5e091af3a18fec2c8b48050869108549

And the code: 和代码:

$items   = [370,370,546,55,55,55];

$unique = array_unique( $items );

$diff = array_diff_assoc( $items, $unique);

print_r( $diff );
echo "\n\n";

$counted = array_count_values($diff);

arsort($counted); //sort descending maintain keys

$occurences = reset($counted); //get the first value (rewinds internal pointer )
$most_frequent = key($counted); //get the key, as we are rewound it's the first key

echo $most_frequent . ' appears ' . ( $occurences + 1 ) . ' time(s)' . PHP_EOL;

Outputs ( both the previous ones ) 输出(均为前一个)

Array
(
    [1] => 370
    [4] => 55
    [5] => 55
)

55 appears 3 time(s)

Oh and if you want the ones that are more then one as a list with no duplicates, then just hit it again with array unique $unique = array_unique($diff); 哦,如果您想要一个以上但没有重复的列表,那么只需使用数组唯一$unique = array_unique($diff);再次击中它$unique = array_unique($diff);

Cheers! 干杯!

You can find same value count like this way. 您可以像这样找到相同的值计数。

print_r(array_count_values($items));

will output 将输出

Array
(
   [370] => 2
   [546] => 1
   [55] => 1
   etc...
)

To get 2 and 307 value you have to do below things. 要获得2和307的值,您必须做以下事情。

$new_arr = array_count_values($items);
$max = max($new_arr);
$new_arr = array_keys($new_arr, max($new_arr)));
echo $new_arr[0]."is repeated (".$max.") times.";

will output 将输出

  307 is repeated (2) times.
$items   = [370,370,546,55,55];
$counted = array_count_values($items);  // count occurrences
$counted=array_intersect ($counted,[max($counted)]);  // only keep elements with highest occurrence
var_export ($counted);  // print to screen 

There is a php function array_count_values that should work in your case. 有一个PHP函数array_count_values应该array_count_values您的情况。

Then you have an array with a count of all values and you can sort and work with them. 然后,您将得到一个包含所有值的数组,您可以对它们进行排序和使用。

https://secure.php.net/manual/en/function.array-count-values.php https://secure.php.net/manual/zh/function.array-count-values.php

$items = array("370","370","546","55");
print_r(array_count_values($items));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM