Ordering PHP arrays based on duplicate values - php

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.

Related

Alternate arrays merge every third item

I have two multi-dimensional arrays (but for simplicity sake let's just say they are single dimension) I need to merge them both into a single one, but alternating elements. First array is larger, and I need second array to be merged in every third item.
Just as an example:
$array1 = array("Hello", "Bonjour", "Hola", "Ciao", "Привет", "Hallo","Nihao");
$array2 = array("World", "Monde", "Mundo");
And I want the final array to look like so
["Hello", "Bonjour", "World", "Hola", "Ciao", "Monde", "Привет", "Hallo", "Mundo", "Nihao"]
If there are much more items in the first array, then it should just keep adding them, regardless whether second array has any left or not.
How would I do that?
In your example, the third item of array2 is inserted after the third item of array1, in first and the second are inserted after the second.
Assuming this was a mistake; and $array1 is always large enough; you could use something like this:
$i=2; while($v = array_shift($array2))
{
array_splice($array1, $i, 0, [$v]);
$i+=3;
}

How to get random item from array only from the specified keys (e.g x key first)

I have a array that i want to select two item as random, And two keys are selected between the key 2 and the key 8.
$arr = array=(1,2,3,4,5,6,7,8,9,10,11,12);
I can get two random item:
$rand_keys = array_rand($arr, 2);
$arr[$rand_keys[0]]; // one
$arr[$rand_keys[0]]; // two
In the above code, It is possible that selected items be from the entire array. Now i want to know how can I limit my choice?
In fact i want to get random item from this array:
array=(2,3,4,5,6,7,8);
You can use array_slice to first take a part of the array before you take random items from it
$part = array_slice($arr, 1, 7); // outputs array(2,3,4,5,6,7,8)

Getting the number of different values in a PHP array

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

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

Categories