Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
857
Q:

PHP array_multisort() function (with example)

Hi everyone,

I'm currently working on a PHP project and I came across the array_multisort() function. I've read the PHP documentation on it, but I'm still having a bit of trouble understanding how it works and how to use it properly. I was hoping someone here could help me out.

To provide some context, I have an array of data that I need to sort in multiple dimensions. Let's say the array looks like this:

```
$myArray = array(
array("Apple", 2, 5),
array("Banana", 4, 2),
array("Orange", 1, 7)
);
```

What I want to achieve is sorting the array based on the first column (the fruit name) in ascending order. If two fruits have the same name, I want to sort them based on the second column (quantity) in descending order. And if the quantity is also the same, I want to sort them based on the third column (price) in ascending order.

I believe array_multisort() can help me achieve this, but I'm not entirely sure how to implement it correctly. Can someone please provide an example using the data I shared?

Any help would be greatly appreciated. Thank you in advance!

All Replies

qlittel

Hey there,

I've actually used the array_multisort() function before, so I can definitely help you out with this. Here's an example of how you can use it to sort your array based on the criteria you mentioned:

php
$myArray = array(
array("Apple", 2, 5),
array("Banana", 4, 2),
array("Orange", 1, 7)
);

// Separate the columns into separate arrays
$fruits = array_column($myArray, 0);
$quantities = array_column($myArray, 1);
$prices = array_column($myArray, 2);

// Sort the arrays in the desired order
array_multisort($fruits, SORT_ASC, $quantities, SORT_DESC, $prices, SORT_ASC, $myArray);

// The sorted array
print_r($myArray);


In this example, we first extract the columns we want to sort on into separate arrays using the array_column() function. Then, we pass these arrays to array_multisort() while specifying the sort order for each column. Finally, the original array ($myArray) is sorted based on the specified criteria.

Running the code above will give you the following sorted array:


Array
(
[0] => Array
(
[0] => Apple
[1] => 2
[2] => 5
)

[1] => Array
(
[0] => Banana
[1] => 4
[2] => 2
)

[2] => Array
(
[0] => Orange
[1] => 1
[2] => 7
)

)


I hope this clarifies how to use array_multisort() in your specific scenario. Let me know if you have any further questions!

sturner

Hey everyone,

I also stumbled upon this thread and thought I could share my experience with the array_multisort() function. It's a very handy function when you need to sort multidimensional arrays based on multiple columns.

From what I understand, you want to sort your array first by the fruit name in ascending order, then by quantity in descending order, and finally by price in ascending order. The example provided by the previous user looks great, but I wanted to offer an alternative approach that might be useful to you.

php
$myArray = array(
array("Apple", 2, 5),
array("Banana", 4, 2),
array("Orange", 1, 7)
);

// Create a temporary array to hold the sorting columns
$sortColumns = array();
foreach ($myArray as $key => $row) {
$sortColumns['fruit'][$key] = $row[0];
$sortColumns['quantity'][$key] = $row[1];
$sortColumns['price'][$key] = $row[2];
}

// Sort the main array based on the sorting columns
array_multisort(
$sortColumns['fruit'], SORT_ASC,
$sortColumns['quantity'], SORT_DESC,
$sortColumns['price'], SORT_ASC,
$myArray
);

// The sorted array
print_r($myArray);


In this approach, we create a temporary array called `$sortColumns` to hold the columns we want to sort. We loop through the original array and populate each sorting column array with the respective values. Then, we pass these arrays along with the main array to `array_multisort()`.

By doing it this way, it provides a bit more flexibility if you need to dynamically define the sorting columns or add more complex sorting criteria.

I hope this helps! Let me know if you have any questions or if there's any other way I can assist you.

schiller.ella

Hello everyone,

I happened to come across this discussion while browsing the forum and thought I could share my personal experience with the array_multisort() function. It's quite a powerful tool when it comes to sorting multidimensional arrays with various sorting criteria.

Regarding the question at hand, I see that you have an array with fruit names, quantities, and prices. The goal is to sort the array based on the fruit name in ascending order. In case of the same fruit name, the sorting should be performed by quantity in descending order, and finally by price in ascending order.

The previous responses have provided excellent examples, but I'd like to share an alternative approach that might interest you. Instead of using the array_column() function to extract columns, we can utilize array_map() and array_column() together to achieve the desired result.

php
$myArray = array(
array("Apple", 2, 5),
array("Banana", 4, 2),
array("Orange", 1, 7)
);

// Extract the columns we want to sort on
$fruits = array_map(function ($row) {
return $row[0];
}, $myArray);

$quantities = array_map(function ($row) {
return $row[1];
}, $myArray);

$prices = array_map(function ($row) {
return $row[2];
}, $myArray);

// Sort the main array based on the sorting columns
array_multisort($fruits, SORT_ASC, $quantities, SORT_DESC, $prices, SORT_ASC, $myArray);

// The sorted array
print_r($myArray);


In this approach, we utilize the array_map() function to extract the specific columns from the original array. This helps us create separate arrays for fruits, quantities, and prices. Then, we pass these arrays to array_multisort() as sorting criteria, following the desired sorting order.

I hope this alternative method provides you with another perspective on utilizing array_multisort(). Feel free to reach out if you have any further questions or need additional assistance!

New to LearnPHP.org Community?

Join the community