1

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.

But how can I output 2 and its related value, so in my example 370?

9
  • 2
    Your array has no key 370. The keys of your array are 0, 1, 2 and 3.
    – CBroe
    Commented Nov 1, 2017 at 9:22
  • What should be the output ? 2 or 370?
    – Rahul
    Commented Nov 1, 2017 at 9:27
  • You means both?
    – Rahul
    Commented Nov 1, 2017 at 9:29
  • max() returns the highest value, it doesn't care how many times this value occurs in the array. 546 is the highest value from $item and 2 is its index (position) in the array. It is not a count.
    – axiac
    Commented Nov 1, 2017 at 9:56
  • 1
    It seems you don't care if there are multiple highest values then. Thanks for the response. Commented Nov 1, 2017 at 10:10

5 Answers 5

2

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

You can try it here

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

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)

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] )

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

One note, is that you will get exactly one less occurrence then is actually present.

UPDATE1

We can easily combine these and account for the missing one. See this fiddle to test that

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);

Cheers!

5
  • Thanks for final clarification...however in your example how can I get the second value too? 370 2 Commented Nov 1, 2017 at 9:59
  • Do you want everything that appears more than once, and this, or this. If you follow. Commented Nov 1, 2017 at 10:01
  • I saw your fiddle as comment in the other answer. It's perfect for me so I can use it also like echo $number_appears_most[1] . ' appears ' . $number_of_occurences[1] . ' time(s)' . PHP_EOL; Commented Nov 1, 2017 at 10:06
  • what about this problem $items = [370,370,370,546,55,55,55]; ? Commented Nov 1, 2017 at 10:16
  • I wouldn't call it a problem it's a tie. You have to decide how you want to handle that. Commented Nov 1, 2017 at 10:18
2

I use array_count_values and from the resulting array use array_keys to get the numbers which appear most (e.g. 370) and array_values to get the number of times it appears (e.g. 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

4
  • Your code is based on the order, see this fiddle sandbox.onlinephpfunctions.com/code/… Commented Nov 1, 2017 at 9:37
  • The correct answer should be 55 appears 3 time(s) however its unchanged. To remedy this you have to sort by the number of occurrences, I would recommend doing arsort ($counted); Commented Nov 1, 2017 at 9:38
  • 1
    See this fiddle where I fixed it for you: sandbox.onlinephpfunctions.com/code/… Commented Nov 1, 2017 at 9:43
  • 1
    Sure, I added an answer with a few other fixes ( or tweaks ) if you don't mind, I was a really good approach though. Give it a look, as I simplified assigng the output. Commented Nov 1, 2017 at 9:58
1

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.

$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.
1
  • I can't use it, I don't know the number 370 Commented Nov 1, 2017 at 9:27
1
$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 
0
0

There is a php function array_count_values that should work in your case.

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

$items = array("370","370","546","55");
print_r(array_count_values($items));
1
  • Thanks, I already know, but how can I use it to output 370 and 2? Commented Nov 1, 2017 at 9:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.