PHP: Most frequent value in array - php

So I have this JSON Array:
[0] => 238
[1] => 7
[2] => 86
[3] => 79
[4] => 55
[5] => 92
[6] => 55
[7] => 7
[8] => 254
[9] => 9
[10] => 75
[11] => 238
[12] => 89
[13] => 238
I will be having more values in the actual JSON file. But by looking at this I can see that 238 and 55 is being repeated more than any other number. What I want to do is get the top 5 most repeated values in the array and store them in a new PHP array.

$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array_count_values() gets the count of the number of times each item appears in an array
arsort() sorts the array by number of occurrences in reverse order
array_keys() gets the actual value which is the array key in the results from array_count_values()
array_slice() gives us the first five elements of the results
Demo
$array = [1,2,3,4,238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array (
0 => 238,
1 => 55,
2 => 7,
3 => 4,
4 => 3,
)

The key is to use something like array_count_values() to tally up the number of occurrences of each value.
<?php
$array = [238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
// Get array of (value => count) pairs, sorted by descending count
$counts = array_count_values($array);
arsort($counts);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1, 9 => 1, ...)
// An array with the first (top) 5 counts
$top_with_count = array_slice($counts, 0, 5, true);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1)
// An array with just the values
$top = array_keys($top_with_count);
// array(238, 55, 7, 75, 89)
?>

Related

Merge 2 arrays in random positions

How would someone merge 2 arrays where $_array_1 is scattered but $_array_2 maintains its original order globally?
I have 2 arrays
$_array_1 = array( a, b, c );
$_array_2 = array( 1, 2, 3, 4, 5, 6 );
$_merged_array = array( c, 1, a, 2, 3, 4, 5, b, 6 );
I know this must be possible but don't have a clue where to start. What is the most efficient method and cleanest?
My interpretation of your post is: Insert every value of array 1 on a random position in array 2. This can be achieved with the following code:
$_array_1 = array( 290, 188, 519 );
$_array_2 = array( 213, 702, 231, 173, 632, 711 );
foreach($_array_1 as $val) {
array_splice($_array_2, rand(0, count($_array_2)), 0, $val);
}
print_r($_array_2); // For example: Array ( [0] => 519 [1] => 213 [2] => 290 [3] => 702 [4] => 231 [5] => 173 [6] => 632 [7] => 188 [8] => 711 )
I don't see how the end result makes sense, but here's what you can do:
Copy the second array in the merged array,
Insert the values in the first array at random positions in the merged array using $insert_position = mt_rand(0, count($merged_array)).
Note that it's not an error that I didn't do count($merged_array) - 1, because we also want the numbers to be randomly added at the end of the array.

How to get array value then implode them into new array in PHP

I have an array:
Array
(
[10 - 20] => 3
[20 - 30] => 43
[30 - 40] => 5
[40 - 50] => 1
[50 - 60] => 0
[60 - 70] => 0
)
I want to make new array that will send via json. Now I have result in json like this(thanks for guys below who answered my question):
{"success":true,"data":["3","43","5","1","0","0"]}
I need json to be {"success":true,"data":[3,43,5,1,0,0]}, cause only with that format data show in line chart of highchart
Sorry for my stupid questionThank You
Update
Here's how you can get your response...
$values = array
(
'10 - 20' => 3,
'20 - 30' => 43,
'30 - 40' => 5,
'40 - 50' => 1,
'50 - 60' => 0,
'60 - 70' => 0,
);
$data = array_values($values);
$response = array('sucess' => true, 'data' => $data);
echo json_encode($response);
Use array_values, this can be done like so:
$values = array
(
'10 - 20' => 3,
'20 - 30' => 43,
'30 - 40' => 5,
'40 - 50' => 1,
'50 - 60' => 0,
'60 - 70' => 0,
);
$values = array_values($values);
$data = json_encode($values);
echo data;
Output:
[3,43,5,1,0,0]
You might be new to php, Declaration of associative array should be like below
$arr=array(
'[10 - 20]' => 3,
'[20 - 30]' => 43,
'[30 - 40]' => 5,
'[40 - 50]' => 1,
'[50 - 60]' => 0,
'[60 - 70]' => 0
);
array('Key'=>value,'key2'=> value);
Write forearch/for/ any loop to get the values, This is useful if you want to extract both key and values at the same time
foreach ($arr as $key=>$value) {
echo "$value <br>";
}
If you want to get only values then use array_values($arr)
You need to use array_values.
PHP Array
$arr = array("10 - 20" => 3,
"20 - 30" => 43,
"30 - 40" => 5,
"40 - 50" => 1,
"50 - 60" => 0,
"60 - 70" => 0
);
Array values from the array.
$arr2 = array_values($arr);
print_r($arr2);
Output Array
Array
(
[0] => 3
[1] => 43
[2] => 5
[3] => 1
[4] => 0
[5] => 0
)
If you need only the values as string then use implode.
echo implode(" , ", $arr2);
Result
3 , 43 , 5 , 1 , 0 , 0

PHP: Filtering too hight and too low numbers in array

I have an array like this: 102, 97, 101, 1, 107, 95, 555.
I need to exclude numbers, which very differ from other. So array should be: 102, 97, 101, 107, 95.
How can I do this in php?
function getAverageArray($min, array $arr){
$arr2 = array($arr[0]);
foreach(array_slice($arr,1) as $val)
if ($val - $arr[0] < $min && $arr[0] - $val < $min)
$arr2[] = $val;
return $arr2;
}
//the minimum difference necessary
$min = 90;
$arr = array(102, 97, 101, 1, 107, 95, 555);
//Array ( [0] => 102 [2] => 97 [3] => 101 [4] => 107 [5] => 95 )
print_r(getAverageArray($min,$arr));
That's possible but you have to set the threshold.
get the average of all the values
exclude values whose `abs(value-avergage) > your_threshold``

PHP - Set first array's values to second array's iterations

I'm trying to make one array set to the iterations of another array. I'm working on a hash algorithm that takes in a user value of the order they want the array. It takes their code and breaks it down into 40 blocks of binary to be converted into hexadecimal. So far I'm able to change the iteration order, but it only takes the last value of the first array and sets as the value for each iteration of the second array.
The first array looks like this (Showing only 10 of the 40 to save space):
Array
(
[0] => 0111
[1] => 1000
[2] => 0110
[3] => 0010
[4] => 0011
[5] => 0001
[6] => 0011
[7] => 0010
[8] => 0011
[9] => 0101
)
The second one is like this:
Array
(
[3] => 0101
[2] => 0101
[1] => 0101
[6] => 0101
[5] => 0101
[4] => 0101
[9] => 0101
[8] => 0101
[7] => 0101
[0] => 0101
)
And here is the PHP code:
$arrayDump = $test->binarySplit($name);
$ordered = array();
$orderKey = array(3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13, 18, 17, 16, 21, 20, 19, 24, 23, 22, 27, 26, 25, 30, 29, 28, 33, 32, 31, 36, 35, 34, 39, 38, 37, 0);
foreach ($orderKey as $key) {
for ($i = $key; $i < count($arrayDump); $i++) {
$ordered[$key] = $arrayDump[$i];
}
}
The class call above isn't too important for this problem that I can tell. The $arrayDump is the first array; $ordered is the second. As you can tell, the second array changes the iteration to be what I want, but it only contains the last value from the first array. I threw it through a loop to try and get each value, but I'm at a loss. Any help would be appreciated.
You don't need the second loop, try this:
foreach ($orderKey as $key => $value) {
$ordered[$key] = $arrayDump[$value];
}

Create a multidimensional array from mysql data

I was wondering how I get a multidimensional array that looks like this:
array(
"wifi" => array(
16 => 499,
32 => 599,
64 => 699
),
"wifi+3G" => array(
16 => 629,
32 => 729,
64 => 829)
);
Out of a mysql table that looks like this:
id, model, grootte, prijs
1, Wifi, 16, 449
2, Wifi, 32, 549
3, Wifi+3G, 16, 499
4, Wifi+3G, 32, 599
Just use one or more fields from your DB results as the keys in the array:
$arr = array();
while($row = mysql_fetch_assoc($result)) {
$arr[$row['somekey']][$row['otherkey']] = $row;
}

Categories