Get top 5 values from multidimensional array in PHP [duplicate] - php

This question already has answers here:
How to sort an array of arrays in php?
(3 answers)
php - usort or array_multisort?
(3 answers)
Get the first N elements of an array?
(5 answers)
PHP: Any function that return first/last N elements of an array
(2 answers)
Closed 2 years ago.
I have the following array:
Array (
[0] => Array (
[count] => 9
[user_id] => 2
)
[1] => Array (
[count] => 25
[user_id] => 1
)
[2] => Array (
[count] => 20
[user_id] => 3 )
[3] => Array (
[count] => 6
[user_id] => 56 )
[4] => Array (
[count] => 2
[user_id] => 37 )
[5] => Array (
[count] => 1
[user_id] => 0
))
This is just a sample. The actual array will contain many more sub arrays.
I need to be able to obtain the top five values from "count" and store them with their associated "user_id".
The final result needs to look something like this:
Array (
[0] => Array (
[count] => 25
[user_id] => 1
)
[1] => Array (
[count] => 20
[user_id] => 3
)
[2] => Array (
[count] => 9
[user_id] => 2
)
[3] => Array (
[count] => 6
[user_id] => 56
)
[4] => Array (
[count] => 2
[user_id] => 37
)
[5] => Array (
[count] => 1
[user_id] => 0
) )
If this can be done by simply re ordering the array, that is fine.
Thanks!

You're looking for usort and array_slice.
Example:
<?php
$array = array(
array(
'count' => 9,
'user_id' => 2
),
array(
'count' => 25,
'user_id' => 1
),
array(
'count' => 20,
'user_id' => 3
),
array(
'count' => 6,
'user_id' => 56
),
array(
'count' => 2,
'user_id' => 37
),
array(
'count' => 1,
'user_id' => 0
)
);
function usort_callback($a, $b)
{
if ( $a['count'] == $b['count'] )
return 0;
return ( $a['count'] > $b['count'] ) ? -1 : 1;
}
usort($array, 'usort_callback');
$top5 = array_slice($array, 0, 5);
print_r($top5);
Outputs:
Array
(
[0] => Array
(
[count] => 25
[user_id] => 1
)
[1] => Array
(
[count] => 20
[user_id] => 3
)
[2] => Array
(
[count] => 9
[user_id] => 2
)
[3] => Array
(
[count] => 6
[user_id] => 56
)
[4] => Array
(
[count] => 2
[user_id] => 37
)
)

usort($array, function ($a, $b) { return $b['count'] - $a['count']; });
$top5 = array_slice($array, 0, 5);

Related

i want to sort an array in php [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 3 years ago.
Array
(
[0] => Array
(
[count] => 9
[slug] => concediat-reangajat
)
[1] => Array
(
[count] => 7
[slug] => salarii-5togo
)
[2] => Array
(
[count] => 10
[slug] => piata-fortei-munca
)
[3] => Array
(
[count] => 3
[slug] => productivitate-angajati
)
[4] => Array
(
[count] => 1
[slug] => stocare-date
)
[5] => Array
(
[count] => 4
[slug] => infrastructura-leadership
)
[6] => Array
(
[count] => 2
[slug] => airbnb-uber
)
[7] => Array
(
[count] => 5
[slug] => salarii-productivitate
)
[8] => Array
(
[count] => 2
[slug] => ceo-resurse-umane
)
[9] => Array
(
[count] => 3
[slug] => hr-ceo
)
[10] => Array
(
[count] => 1
[slug] => burnout-tratament
)
[11] => Array
(
[count] => 1
[slug] => angajati-vanzarea-afacerii
)
[12] => Array
(
[count] => 1
[slug] => job-linkedin
)
[13] => Array
(
[count] => 1
[slug] => primul-faliment
)
[14] => Array
(
[count] => 3
[slug] => salariu-mic
)
[15] => Array
(
[count] => 1
[slug] => varsta-programatori
)
)
i want to sort this array based on decending order of count
this array contains arrays of data
count and a slug variable
i want to sort whole array based on descending order of count
how i can sort this.. anybody can help me
i tried usort,ksort etc
am new in programming
not getting a solution
please help me
i tried for a solution
but its didnt work
anybody can solve it?
usort with callback will work for you.
usort($array, function($a, $b) {
return $b['count'] - $a['count']; # $a['count'] - $b['count']; will sort asc
});
WORKING DEMO: https://3v4l.org/SUa2F
OR do simple with arsort($array);
WORKGING DEMO: https://3v4l.org/bnWuo
Reverse sort by count descending (and name ascending for like counts):
<?php
$items =
[
['count'=>2, 'name' => 'Janitor'],
['count'=>5, 'name' => 'Plumber'],
['count'=>1, 'name' => 'Scrubber'],
['count'=>3, 'name' => 'Winkle Picker'],
['count'=>3, 'name' => 'Window Cleaner']
];
usort($items, function($a, $b) {
$compare = $b['count'] <=> $a['count'];
if($compare == 0)
$compare = $a['name'] <=> $b['name'];
return $compare;
});
var_export($items);
Output:
array (
0 =>
array (
'count' => 5,
'name' => 'Plumber',
),
1 =>
array (
'count' => 3,
'name' => 'Window Cleaner',
),
2 =>
array (
'count' => 3,
'name' => 'Winkle Picker',
),
3 =>
array (
'count' => 2,
'name' => 'Janitor',
),
4 =>
array (
'count' => 1,
'name' => 'Scrubber',
),
)

Sort array based on different columns

I created an array based on a mysql table
Array (
[0] => Array ( [id] => 1 [parent_ID] => 0 )
[1] => Array ( [id] => 2 [parent_ID] => 0 )
[2] => Array ( [id] => 3 [parent_ID] => 2 )
[3] => Array ( [id] => 4 [parent_ID] => 2 )
[4] => Array ( [id] => 5 [parent_ID] => 2 )
[5] => Array ( [id] => 6 [parent_ID] => 1 )
[6] => Array ( [id] => 7 [parent_ID] => 1 )
[7] => Array ( [id] => 8 [parent_ID] => 1 )
[8] => Array ( [id] => 9 [parent_ID] => 1 )
I want to create a new array, where the order of the parent_ID is based on the ID. If the parent_ID from an array is “1”, than it needs to be placed directly after the array that has ID “1”. The output of the new array needs to be like this:
Array (
[0] => Array ( [id] => 1 [parent_ID] => 0 )
[1] => Array ( [id] => 6 [parent_ID] => 1 )
[2] => Array ( [id] => 7 [parent_ID] => 1 )
[3] => Array ( [id] => 8 [parent_ID] => 1 )
[4] => Array ( [id] => 9 [parent_ID] => 1 )
[5] => Array ( [id] => 2 [parent_ID] => 0 )
[6] => Array ( [id] => 3 [parent_ID] => 2 )
[7] => Array ( [id] => 4 [parent_ID] => 2 )
[8] => Array ( [id] => 5 [parent_ID] => 2 )
I tried to order my array by using the usort function, but that will only order the parent_ID or the ID column. Is it possible with PHP to sort an array like the example?
As said in my comment to the question your proposed result is bogus...
But here is some simple algorithm constructing such output:
<?php
$input = [
['id' => 1, 'parent_ID' => 0],
['id' => 2, 'parent_ID' => 0],
['id' => 3, 'parent_ID' => 2],
['id' => 4, 'parent_ID' => 2],
['id' => 5, 'parent_ID' => 2],
['id' => 6, 'parent_ID' => 1],
['id' => 7, 'parent_ID' => 1],
['id' => 8, 'parent_ID' => 1],
['id' => 9, 'parent_ID' => 1]
];
$data = [];
$output = [];
array_walk ($input, function($entry) use (&$data) {
$data[$entry['parent_ID']][] = $entry;
});
array_walk ($data[0], function($entry) use ($data, &$output) {
$output[] = $entry;
foreach ($data[$entry['id']] as $child) {
$output[] = $child;
}
});
print_r($output);
The output of executing that obviously is:
Array
(
[0] => Array
(
[id] => 1
[parent_ID] => 0
)
[1] => Array
(
[id] => 6
[parent_ID] => 1
)
[2] => Array
(
[id] => 7
[parent_ID] => 1
)
[3] => Array
(
[id] => 8
[parent_ID] => 1
)
[4] => Array
(
[id] => 9
[parent_ID] => 1
)
[5] => Array
(
[id] => 2
[parent_ID] => 0
)
[6] => Array
(
[id] => 3
[parent_ID] => 2
)
[7] => Array
(
[id] => 4
[parent_ID] => 2
)
[8] => Array
(
[id] => 5
[parent_ID] => 2
)
)
However I would like to make some comments to your situation:
you really should not store a value of 0 for the first elements. They do not have a parent, if I get your situation right, so that value should actually be null, not 0.
you should try to order your data right in the SQL query
you should rethink the overall approach since apparently you have huge issues sorting your data with the given data model. That typically is a sign of a modelling approach that should be reworked.

pop and push from one associative array to another array

I want to pop from array2 and want to push in array1.
But according to some custom requirement.
Now in array1 there is 1st key's readingOrder is 1 and 2nd key's readingOrder is 4.
So i want to push between this two key from array2's first two key.And same process for all other.
and my final array must be like array3.
for example in array1 key[0] readingOrder is 1 and key[1]'s 4. Now i want to push another two key from array2.
for array1 key[2] reading order is 6. so before this key i want to push another one key from array2 and same for further...
array1 is like below
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 4
[id] => 76
)
[2] => Array
(
[readingOrder] => 6
[id] => 80
)
)
array2 is like below
Array
(
[0] => Array
(
[id] => 81
[readingOrder] => 2
)
[1] => Array
(
[id] => 82
[readingOrder] => 5
)
[2] => Array
(
[id] => 84
[readingOrder] => 7
)
[3] => Array
(
[id] => 85
[readingOrder] => 8
)
[4] => Array
(
[id] => 86
[readingOrder] => 9
)
[5] => Array
(
[id] => 87
[readingOrder] => 10
)
[6] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Output array3:
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[id] => 81
[readingOrder] => 2
)
[2] => Array
(
[id] => 82
[readingOrder] => 5
)
[3] => Array
(
[readingOrder] => 4
[id] => 76
)
[4] => Array
(
[id] => 84
[readingOrder] => 7
)
[5] => Array
(
[readingOrder] => 6
[id] => 80
)
[6] => Array
(
[id] => 85
[readingOrder] => 8
)
[7] => Array
(
[id] => 86
[readingOrder] => 9
)
[8] => Array
(
[id] => 87
[readingOrder] => 10
)
[9] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Thanks..
You can build your array like that:
$current = 1;
$arr3 = [];
while ( $arr1 && $arr2 ) {
if ( $arr1[0]['readingOrder'] > $current )
$arr3[] = array_shift($arr2);
else
$arr3[] = array_shift($arr1);
$current++;
}
$arr3 = array_merge($arr3, $arr1, $arr2);
print_r($arr3);
Note that this code is destructive for $arr1 and $arr2. If you want to preserve them, copy them before and use the copies instead.
You can do this with usort. First you need to merge the arrays:
$a1 = [
[
'readingOrder' => 1,
'id' => 78
],
[
'readingOrder' => 4,
'id' => 76
],
[
'readingOrder' => 6,
'id' => 80
]
];
$a2 = [
[
'readingOrder' => 2,
'id' => 81
],
[
'readingOrder' => 5,
'id' => 82
],
[
'readingOrder' => 7,
'id' => 84
],
[
'readingOrder' => 8,
'id' => 85
]
];
$a3 = array_merge($a1, $a2);
Then you need to use usort:
usort($a3, function($a,$b) {
if ($a['readingOrder'] == $b['readingOrder']) return 0;
return $a['readingOrder'] < $b['readingOrder'] ? -1 : 1;
});
In PHP 7 you can now use the spaceship operator, which would make the code more clean. Like so:
usort($a3, function($a,$b) {
return $a[0] <=> $b[0];
});
This will then return:
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 2
[id] => 81
)
[2] => Array
(
[readingOrder] => 4
[id] => 76
)
[3] => Array
(
[readingOrder] => 5
[id] => 82
)
[4] => Array
(
[readingOrder] => 6
[id] => 80
)
[5] => Array
(
[readingOrder] => 7
[id] => 84
)
[6] => Array
(
[readingOrder] => 8
[id] => 85
)
)

array_push or array_unshift multiple rows to top of same array PHP

I have an array:
[0] => Array
(
[Id] => 1
[Order] => 1
[ContentGroupId] => 10
[ContentGroupIsNew] => 0
)
[1] => Array
(
[Id] => 2
[Order] => 2
[ContentGroupId] => 11
[ContentGroupIsNew] => 0
)
[2] => Array
(
[Id] => 3
[Order] => 3
[ContentGroupId] => 12
[ContentGroupIsNew] => 1
)
[3] => Array
(
[Id] => 4
[Order] => 4
[ContentGroupId] => 13
[ContentGroupIsNew] => 1
)
[4] => Array
(
[Id] => 5
[Order] => 5
[ContentGroupId] => 14
[ContentGroupIsNew] => 0
)
The default order is by [Order] I want to re-sort this array so that it orders by [ContentGroupIsNew] = 1 at the top (if any exist, in this sample 2 do exist), but keep the existing order of the remainder of elements.
If I use a usort function, I can get the [ContentGroupIsNew] = 1 but then the remainder of elements seems to get randomly ordered and not keep true to the original [Order] value.
So the final result should look like this:
[0] => Array
(
[Id] => 3
[Order] => 3
[ContentGroupId] => 12
[ContentGroupIsNew] => 1
)
[1] => Array
(
[Id] => 4
[Order] => 4
[ContentGroupId] => 13
[ContentGroupIsNew] => 1
)
[2] => Array
(
[Id] => 1
[Order] => 1
[ContentGroupId] => 10
[ContentGroupIsNew] => 0
)
[3] => Array
(
[Id] => 2
[Order] => 2
[ContentGroupId] => 11
[ContentGroupIsNew] => 0
)
[4] => Array
(
[Id] => 5
[Order] => 5
[ContentGroupId] => 14
[ContentGroupIsNew] => 0
)
PHP code:
function sort_array($b, $a) {
return $a['ContentGroupIsNew'] - $b['ContentGroupIsNew'];
}
usort($array, 'sort_array');
foreach ($data as $key => $row) {
$return_fare[$key] = $row['ContentGroupIsNew'];
$one_way_fare[$key] = $row['Id'];
}
array_multisort($return_fare, SORT_DESC, $one_way_fare, SORT_ASC, $data);
print_r($data);
found this solution here : PHP sort array by two field values
please try this
function myCmp($a, $b)
{
return strcmp($b["ContentGroupIsNew"], $a["ContentGroupIsNew"]);
}
uasort($array, "myCmp")
echo "<pre>"; print_r($array);

Sort php multidimensional array by key value

I have the below multidimesional array.
Array
(
[2] => Array
(
[66] => Array
(
[id] => 66
[count] => 9
)
[255] => Array
(
[id] => 255
[count] => 20
)
)
[1] => Array
(
[59] => Array
(
[id] => 59
[count] => 14
)
[255] => Array
(
[id] => 255
[count] => 73
)
)
)
I want to sort the inner array by value of count key in descending order.
How can I achieve it ?
Thanks.
try below solution:
<?php
$array = Array
(
'2' => Array
(
'66' => Array
(
'id' => 66 ,
'count' => 9
),
'255' => Array
(
'id' => 255,
'count' => 20
)
),
'1' => Array
(
'59' => Array
(
'id' => 59,
'count' => 14
),
'255' => Array
(
'id' => 255,
'count' => 73
)
)
);
echo '<pre>';
foreach($array as &$ar){
usort($ar, function($a, $b) {
return $b['count'] - $a['count'];
});
}
print_r($array);
Output:
Array
(
[2] => Array
(
[0] => Array
(
[id] => 255
[count] => 20
)
[1] => Array
(
[id] => 66
[count] => 9
)
)
[1] => Array
(
[0] => Array
(
[id] => 255
[count] => 73
)
[1] => Array
(
[id] => 59
[count] => 14
)
)
)
Here is an example:
Sort Multi-Dimensional Array By Value In PHP
https://paulund.co.uk/sort-multi-dimensional-array-value

Categories