Sorting this multidimention array in php [duplicate] - php

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

Related

How to sort array based on subarray value in PHP [duplicate]

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.

Check if array contain other array (for multidimensional array) php [duplicate]

This question already has answers here:
PHP: Check if an array contains all array values from another array
(5 answers)
Closed 4 years ago.
I am looking forward to compare two arrays in PHP.
For example, I have array A:
Array
(
[0] => Array
(
[option_id] => 19
[sub_option_id] => 57
)
[1] => Array
(
[option_id] => 1093
[sub_option_id] => 3582
)
[2] => Array
(
[option_id] => 1093
[sub_option_id] => 57
)
)
And array B:
Array
(
[0] => Array
(
[order_option_detail] => Array
(
[0] => Array
(
[option_id] => 19
[sub_option_id] => 57
)
[1] => Array
(
[option_id] => 1093
[sub_option_id] => 57
)
[2] => Array
(
[option_id] => 1093
[sub_option_id] => 3582
)
)
)
[1] => Array
(
[order_option_detail] => Array
(
[0] => Array
(
[option_id] => 1
[sub_option_id] => 2
)
)
)
)
By looking at the data structure, I can see that array B contains array A. How can I achieve the same analysis using PHP, ie how to check array B contain array A?
Please help me if you know!
Thank you so much!
You can use the following function for array compare:
function array_equal($a, $b) {
if (!is_array($a) || !is_array($b) || count($a) != count($b))
return false;
$a = array_map("json_encode", $a);
$b = array_map("json_encode", $b);
return array_diff($a, $b) === array_diff($b, $a); // mean both the same values
}
And then use it as:
$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
if (array_equal($detail, $arrayA)) {
// Do what ever
}
}
From arrayB you only need 'order_option_detail'.
So if we use array_column we can get those isolated.
$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
if($detail === $arrayA){
// Do something
}
}
https://3v4l.org/TW670

How to sort 2 arrays by the value [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 7 years ago.
I have this array:
Array (
[order] => Array ( [0] => 2 [1] => 1 )
[eventid] => Array ( [0] => id_1 [1] => id_2 )
)
Now I would like to get:
Array (
[order] => Array ( [0] => 1 [1] => 2 )
[eventid] => Array ( [0] => id_2 [1] => id_1 )
)
Basically I would like to sort arrays by value of order.
You will need to use the usort function to be able to do this. (See the documentation)
I would recommend another Array structure though, something like this:
Array (
[0] => Array ( [order] => 2, [eventid] => id_x )
[1] => Array ( [order] => 1, [eventid] => id_y )
)
Then you could a function like this one to sort your array (PHP 5.3 or greater):
function array_sort_by(&$array, $key, $descending = false) {
$sortByKey =
function ($a, $b) use ($key, $descending) {
if ($a[$key] === $b[$key]) {
return 0;
}
$return = $a[$key] < $b[$key] ? -1 : 1;
return ($descending ? -1 * $return : $return);
};
usort($array, $sortByKey);
}
You would then call the following:
array_sort_by($yourArray, 'order');
You can use asort. While it can cover your case, usort might be a better solution in the long run.
$arr = Array (
"order" => Array ( 0 => 6, 1 => 1,2=>43),
"eventid" => Array ( 0 => 5, 1 => 1,2=>54,3=>0)
);
foreach ($arr as $key => &$value) {
asort($value);
}

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

How do I sort an array by a specific value inside of it? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting an array based on its value
I want to sort an array by a value inside of it. I tried usort but it leads to some unexpected results in actually changing the value of the output instead of just shifting elements in the array.
Below is the array I want to sort:
Array
(
[element1] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 7
[totalPreregistrationsToDate] => 26
[pas] => Array
(
[0] => Array
(
[id] => 119
)
)
)
[element2] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 0
[totalPreregistrationsToDate] => 58
[pas] => Array
(
[0] => Array
(
[id] => 107
)
)
)
... element3, 4, etc...
I want to sort by the "totalPreregistrations" number so that element2 goes above element1 if element2's totalPreregistrations count goes above element1's.
And of course I want the sub-arrays to be retained as well.
Thank you!
Documentation: uasort()
function mySort($a, $b) {
if( $a['totalPreregistrations'] == $b['totalPreregistrations'] ) {
return 0;
} else if( $a['totalPreregistrations'] > $b['totalPreregistrations'] ) {
return 1;
} else {
return -1;
}
}
uasort($array, 'mySort');
You can use uasort instead of usort
uasort($array, function ($a, $b) {
$a = $a['totalPreregistrations'];
$b = $b['totalPreregistrations'];
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});
echo "<pre>";
print_r($array);
Output
Array
(
[element2] => Array <-------------------------- element key remains intact
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 0
[totalPreregistrationsToDate] => 58
[pas] => Array
(
[0] => Array
(
[id] => 107
)
)
)
[element1] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 7
[totalPreregistrationsToDate] => 26
[pas] => Array
(
[0] => Array
(
[id] => 119
)
)
)
)

Categories