PHP | Sorting Complex Multidimensional Array

Suppose we have a complex array like below:

$products = array(
array(
'product' => array('id' => 1, 'name' => 'Jquery tutorial book'),
'product_details' => array('cost' => 10, 'edition' => 'first')
),
array(
'product' => array('id' => 2, 'name' => 'javascript tutorial book'),
'product_details' => array('cost' => 20, 'edition' => 'first')
),
array(
'product' => array('id' => 3, 'name' => 'C++ tutorial book'),
'product_details' => array('cost' => 30, 'edition' => 'third')
),
array(
'product' => array('id' => 4, 'name' => 'C Programming book'),
'product_details' => array('cost' => 40, 'edition' => 'third')
),
array(
'product' => array('id' => 5, 'name' => 'computer fundamentals book'),
'product_details' => array('cost' => 20, 'edition' => 'first')
)
);

Suppose we want to sort the array by ‘name’ of ‘product’. To sort an array, simply use the following code:

foreach ($products as $key => $row)
{
$column[$key]  = $row['product']['fullname']);
}
array_multisort($column, SORT_ASC, $products);

array_multisort function requires the array of columns to be sorted. So, we first will pass $columns array as first parameter and then we will pass sort order as second parameter in function and then actual array.

In the above case, we will get array in following order:

1. C Programming book
2. C++ tutorial book
3. Jquery tutorial book
4. Computer fundamentals book
5. Javascript tutorial book

Here initially, all names with capital letters are sorted and then product names with lowercase letters are sorted because according to ASCII values, upper case letters have lower ASCII values and lower case letters have high ASCII values.

If we want array to be sorted alphabetically and we don’t want the resulted array in the above manner, then we can change the code as following:

foreach ($products as $key => $row)
{
$column[$key]  = strtolower($row['product']['fullname']);
}

Finally use the array_multisort function.

Now, array will be in the following order:

$products = $products = array(
array(
'product' => array('id' => 4, 'name' => 'C Programming book'),
'product_details' => array('cost' => 40, 'edition' => 'third')
),
array(
'product' => array('id' => 3, 'name' => 'C++ tutorial book'),
'product_details' => array('cost' => 30, 'edition' => 'third')
),
array(
'product' => array('id' => 5, 'name' => 'computer fundamentals book'),
'product_details' => array('cost' => 20, 'edition' => 'first')
),
array(
'product' => array('id' => 2, 'name' => 'javascript tutorial book'),
'product_details' => array('cost' => 20, 'edition' => 'first')
),
array(
'product' => array('id' => 1, 'name' => 'Jquery tutorial book'),
'product_details' => array('cost' => 10, 'edition' => 'first')
)
);

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *