How to sort php array [duplicate] - php

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'];
});

Related

PHP multi dimentional array sum and make unique [duplicate]

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

Sorting multidimensional array on inner value php [duplicate]

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');

Sorting this multidimention array in php [duplicate]

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

how to change index of an array in php [duplicate]

This question already has answers here:
Built-in PHP function to reset the indexes of an array?
(4 answers)
Closed 8 years ago.
I want to change the index of an array after doing some operations
My actual output is
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[2] => Array
(
[0] => 1
[1] => 7
)
[5] => Array
(
[0] => 1
[1] => 7
)
)
I want to be something like this
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[1] => Array
(
[0] => 1
[1] => 7
)
[2] => Array
(
[0] => 1
[1] => 7
)
)
How to achieve this in php???? Thanks in advance
Extract only the values to a new array and the keys will be indexed from 0:
$array = array_values($array);
The array_values() function returns only values from array.
Thus, resetting all the array keys to numeric starting from 0, 1, 2, ...
You can do it using
$array = array_values($array);
http://php.net/manual/en/function.array-values.php
you can use the "array_values" Function for that.
Please refer this link http://www.php.net/manual/en/language.types.array.php

Remove duplicate values from an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove duplicate values from a multi-dimensional array in PHP
I would like to know how to remove the duplicate array values from the following
Array (
[0] => Array ( )
[1] => Array (
[0] => 7
[1] => 8
)
[2] => Array (
[0] => 7
)
[3] => Array (
[0] => 8
)
)
Use array_unique PHP function.
array_unique($variable);

Categories