InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Sorting Multiple Arrays in PHP
PHP

Sorting Multiple Arrays in PHP

InfinityCoder November 20, 2016

You want to sort multiple arrays or an array with multiple dimensions.

Use array_multisort():
To sort multiple arrays simultaneously, pass multiple arrays to array_multisort():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$colors = array('Red', 'White', 'Blue');
$cities = array('Boston', 'New York', 'Chicago');
 
array_multisort($colors, $cities);
print_r($colors);
print_r($cities);
Array
(
    [0] => Blue
    [1] => Red
    [2] => White
)
Array
(
    [0] => Chicago
    [1] => Boston
    [2] => New York
)

To sort multiple dimensions within a single array, pass the specific array elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$stuff = array('colors' => array('Red', 'White', 'Blue'),
               'cities' => array('Boston', 'New York', 'Chicago'));
 
array_multisort($stuff['colors'], $stuff['cities']);
print_r($stuff);
Array
(
    [colors] => Array
(
            [0] => Blue
            [1] => Red
            [2] => White
)
    [cities] => Array
(
            [0] => Chicago
            [1] => Boston
            [2] => New York
)
 
)

To modify the sort type, as in sort(), pass in SORT_REGULAR, SORT_NUMERIC, or SORT_STRING after the array.

To modify the sort order, unlike in sort(), pass in SORT_ASC or SORT_DESC after the array.

You can also pass in both a sort type and a sort order after the array.

The array_multisort() function can sort several arrays at once or a multidimensional array by one or more dimensions.

The arrays are treated as columns of a table to be sorted by rows.

The first array is the main one to sort by; all the items in the other arrays are reordered based on the sorted order of the first array.

If items in the first array compare as equal, the sort order is determined by the second array, and so on.
The default sorting values are SORT_REGULAR and SORT_ASC, and they’re reset after each array, so there’s no reason to pass either of these two values, except for clarity:

1
2
3
4
$numbers = array(0, 1, 2, 3);
$letters = array('a', 'b', 'c', 'd');
array_multisort($numbers, SORT_NUMERIC, SORT_DESC,
                $letters, SORT_STRING , SORT_DESC);

This example reverses the arrays.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Removing HTML and PHP Tags in PHP
You want to remove HTML and PHP tags from a …

Removing HTML and PHP Tags in PHP

Getting and Setting File Timestamps in PHP
You want to know when a file was last accessed …

Getting and Setting File Timestamps in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap