This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
Say I have the following array, how can I sort it on sort_by?
Array
(
[10] => Array
(
[Masthead_slide] => Array
(
[id] => 1456464564
[sort_by] => 1
)
)
[6] => Array
(
[Masthead_slide] => Array
(
[id] => 645454
[sort_by] => 10
)
)
[7] => Array
(
[Masthead_slide] => Array
(
[id] => 4547
[sort_by] => 5
)
)
)
Try by this
function sortByOrder($a, $b) {
return $a['sort_by'] - $b['sort_by'];
}
usort($arr, 'sortByOrder');
Related
This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
How to sort array in descending order based on a specific value [duplicate]
(3 answers)
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 6 days ago.
How to sort given array by [members_count] value? Descending, from largest to smallest
Array
(
[0] => Array
(
[0] => Array
(
[id] => 13317
[members_count] => 46142
[is_closed] => 0
[type] => page
[is_admin] => 0
[is_member] => 0
)
[1] => Array
(
[id] => 75960
[members_count] => 61805
[is_closed] => 0
[type] => page
[is_admin] => 0
[is_member] => 0
)
)
)
usort($data, function ($a, $b) {
return $a['members_count'] <=> $b['members_count'];
});
This question already has answers here:
PHP Sort Array By SubArray Value
(8 answers)
Closed 3 years ago.
I have array like this
Array
(
[0] => Array
(
[id] => 16059
[product_id] => 4013
[Product] => Array
(
[id] => 4013
[name] => XYZ
)
)
[1] => Array
(
[id] => 16060
[product_id] => 4462
[Product] => Array
(
[id] => 4462
[name] => MNOP
)
)
[2] => Array
(
[id] => 16061
[product_id] => 4473
[Product] => Array
(
[id] => 4473
[name] => ABCD
)
)
)
How to short this array using Product > name in ascending order. I can do using for-each loop, but there is any method to without loop ?
Use usort() with strcmp():
usort($array, function($a, $b) {
return strcmp($a['Product']['name'] , $b['Product']['name']);
});
print_r($array);
Output:- https://3v4l.org/Cb5S5
Try -
usort($array, function($a, $b) {
return $a['Product']['name'] > $b['Product']['name'];
});
usort()
Here is the snippet,
$t = [];
foreach ($arr as $key => $value) {
$t[$key] = $value['Product']['name'];
}
array_multisort($t, SORT_ASC, $arr);
First, fetch the data of that name and create an array.
Pass the relevant array for sorting criteria to a multidimensional array.
Demo.
This question already has answers here:
Associative array, sum values of the same key
(5 answers)
Closed 3 years ago.
Following is my array. I need to add its Sum field according to each emp_firstname. Some has only one time coming, some coming two times. how can we sum the field and make the array unique?
Array
(
[0] => Array
(
[emp_firstname] => Alistair
[non_pm] => AMZ
[sum] => 2
)
[1] => Array
(
[emp_firstname] => Shakkeer
[non_pm] => SHK
[sum] => 3
)
[2] => Array
(
[emp_firstname] => Waqas
[non_pm] => WAS
[sum] => 12
)
[3] => Array
(
[emp_firstname] => Zain
[non_pm] => ZAI
[sum] => 9
)
[4] => Array
(
[emp_firstname] => Shakkeer
[gud_pmeditor] => SHK
[sum] => 4
)
[5] => Array
(
[emp_firstname] => Zain
[gud_pmeditor] => ZAI
[sum] => 2
)
)
You can get the desired result using this approach
$res=[];
foreach($arr as $val){
if(array_key_exists($val['emp_firstname'], $res))
$res[$val['emp_firstname']]['sum'] = ($res[$val['emp_firstname']]['sum'] + $val['sum']);
else
$res[$val['emp_firstname']] = $val;
}
Live Demo
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I have a multidimensional array that needs to be reoreder, the array look like this :
[products] => Array
(
[149] => Array
(
[name] => Ichikami1
[qty] => 2
)
[150] => Array
(
[name] => Ichikami2
[qty] => 4
)
[377] => Array
(
[name] => BCL
[qty] => 2
)
)
inside the child array there is 'qty' index, i want to sort the child array by 'qty' index in descending order, so it will look like this:
[products] => Array
(
[0] => Array
(
[name] => Ichikami2
[qty] => 4
)
[1] => Array
(
[name] => Ichikami1
[qty] => 2
)
[2] => Array
(
[name] => BCL
[qty] => 2
)
)
is there a way to do this?
Simply by using array_multisort(), You can do this like:
$qty = array();
foreach ($products as $key => $row)
{
$qty[$key] = $row['qty'];
}
array_multisort($qty, SORT_DESC, $products);
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 8 years ago.
I have tried this problem
how do you sort this array using the value that is elements in [1].
I would also appreciate if someone demonstrate how print each key and it's value of this
array
Array
(
[0] => Array
(
[0] => 9
[1] => 0
)
[1] => Array
(
[0] => 10
[1] => 290
)
[2] => Array
(
[0] => 12
[1] => 852
)
[3] => Array
(
[0] => 13
[1] => 9
)
[4] => Array
(
[0] => 14
[1] => 896
)
)
please help
You could use uasort
function cmp($a, $b) {
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
uasort($array, 'cmp');
To print each key, value... just iterate over it with a foreach