Getting the number of different values in a PHP array - php

I need to know the number of different values in an array. For example if it contains (5, 5, 5) then I should get 1. If it contains (4, 4, 2, 2) I should get 2. If (5, 5, 5, 5, 2, 2, 1, 5) then 3.
Is there a function that can achieve that directly in PHP?

echo count(array_unique($arr));
you can use array_unique which returns unique elements of array (result is also array type) and then count on result

Two simple ways to achieve that:
$diff_count = count(array_unique($input_arr));
$diff_count = count(array_flip($input_arr)); <-- recommended (performance)
array_unique() :
Removes duplicate values from an array - you can add adittional flags such as:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
array_flip():
Exchanges all keys with their associated values in an array - so duplicates keys are overwritten and you get a final array with the unique values as keys.
Flipping it twice will result in an array with unique values and the highest keys there were in the original input array.
More reading:
array_unique.
array_flip.
Benchmark:
Since the array_flip uses only O(n) exactly its much faster - using an array of 10,000 numbers as an input:
array_unique + count: ~28.615 ms.
array_flip + count: ~0.5012 ms. (winner)

Is there a function that can achieve that directly in PHP?
The closest one would be array_unique() but here is a simple function to achieve what you are looking for:
$arr = array(5,5,5,5,2,2,1,5);
function array_diff_values( $array ) {
return count( array_unique($array) );
}
$count = array_diff_values( $arr );
result of $count is 3

Related

Using array_slice() to get first few values

I'd like to use an array_slice to get every value in an array iterated over, except for the current one, the one before it, and all the others until the end of the array.
So for example, say I have 5 elements:
[1, 2, 3, 4, 5]
I want a way for a condition to fire on 3, cut it out, and then also cut out 4 and 5. I have this, but I don't think it's correct:
$items = array_slice($items, $itemcount - 1);
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
So if you want to get the 3,4,5 your offset should be 2, because the array key ( starts from 0 ex: 0,1,2,3,4). i didn't use length in this one because i want to get it till the end (5)
$items = array_slice($items, 2);
If you want to grag 1,2 it should be like this. ( i use length 2 because i want to get the first 2 keys)
$items = array_slice($items, 0, 2);
Example

How do you get the top 10 items in an array (PHP)?

What is the best way to get the top 10 items in an array, I have an array that has several hundred items and I would like to get the top 10 items (most duplicated items) from the array using PHP, any suggestions?
This should do the trick:
$inputArray = array('orange','banana', 'banana', 'banana', 'pear', 'orange', 'apples','orange', 'grape', 'apple');
$countedArray = array_count_values($inputArray);
arsort($countedArray);
$topTen = array_slice($countedArray, 0, 10);
The above will return the array in order of the items with the most occurrences.
Try using php's array_count_values() to get the number of occurrences of each value in the array, in conjunction with arsort() to sort the array by the highest frequency values. Then you can take the top 10 most frequent values of the array with array_slice().
$dataArr = array('test', 4, 15.2, ...); // Input array with all data
$frequencies = array_count_values($dataArr);
arsort($frequencies); // Sort by the most frequent matches first.
$tenFrequencies = array_slice($frequencies, 0, 10, TRUE); // Only get the top 10 most frequent
$topTenValues = array_keys($tenFrequencies);
Note: We need to use array_keys() to get the final values because array_count_values() "returns an array using the values of the input array as keys and their frequency in input as values."

sort associative array by value and get ten greatest keys

I have an associative array from a loop where I store a user_id (key) and a score (value)
$scores[$user_id] = $score;
I was wondering, after this loop has finished, how can I sort the values (get the ten highest scores) and get their keys.
asort($scores);
$tenHighest = array_slice($scores, -10, null, true);
$tenHighestKeys = array_keys($tenHighest);
Use asort() (to preserve the keys) and array_slice() to get the top 10.
asort($scores, SORT_NUMERIC);
$top10 = array_slice($sort, -10);
Use array_keys() to get the keys, like Deceze did.
asort($scores);
http://php.net/manual/en/function.asort.php

Ordering PHP arrays based on duplicate values

I have an array which contains duplicate values. I want to sort the array so that the values with the most duplicates appear first in line. Here's an example of my array:
array(1, 2, 3, 2, 1, 2, 2);
I want to sort this array so that it orders itself based on the amount of duplicates into the following:
array(2, 1, 3);
'2' has the most duplicates so it's sorted first, followed by values will less duplicates. Does anyone know how I can accomplish this?
$acv=array_count_values($array); // 1=>2, 2=>3,3=>1
arsort($acv); //save keys, 2=>3, 1=>2, 3=>1
$result=array_keys($acv); //get only keys 2,1,3
traverse the array, and increment OCCURENCES in another key-value associative array.
sort the second array by values.
get array_keys of second array.

How to get the last n items in a PHP array as another array?

How to I get an array of the last n items of another array in PHP?
$n is equal to the number of items you want off the end.
$arr = array_slice($old_arr, -$n);
You can use array_slice:
$arr = array_slice($old_arr, -$n, $n, true);
If the array indices are meaningful to you, remember that array_slice will reset and reorder the numeric array indices. You need the preserve_keys flag (4th parameter) set to true to avoid this.

Categories