Php sort multi dimensional array

Long not post, and now I want post again about how to sort multi dimensional array. Example I’ve array like this:

$arr = [
	[
	'title' => 'Tree',
        'num' => 3
	],
	[
		'title' => 'Four',
        'num' => 4
	],
	[
		'title' => 'One',
        'num' => 1
	],
	[
		'title' => 'Two',
        'num' => 2
	]
];
echo "<pre>";
print_r($arr);
echo "</pre>";

And if I run this code, the result is:

Array
(
    [0] => Array
        (
            [title] => Tree
            [num] => 3
        )

    [1] => Array
        (
            [title] => Four
            [num] => 4
        )

    [2] => Array
        (
            [title] => One
            [num] => 1
        )

    [3] => Array
        (
            [title] => Two
            [num] => 2
        )

)

And the question is:

How to sort an array, and I want sort by “num”?

So for the answer is, you can use usort function from php, for more info read this http://php.net/manual/en/function.usort.php. So for above problem, now we must add this code:

$arr = [
	[
		'title' => 'Tree',
        'num' => 3
	],
	[
		'title' => 'Four',
        'num' => 4
	],
	[
		'title' => 'One',
        'num' => 1
	],
	[
		'title' => 'Two',
        'num' => 2
	]
];
echo "Before: <pre>";
print_r($arr);
echo "</pre>";

usort($arr, function($a, $b) {
    return $a['num'] - $b['num'];
});

echo "After: <pre>";
print_r($arr);
echo "</pre>";

And if we run that code, the result is:

Before:

Array
(
    [0] => Array
        (
            [title] => Tree
            [num] => 3
        )

    [1] => Array
        (
            [title] => Four
            [num] => 4
        )

    [2] => Array
        (
            [title] => One
            [num] => 1
        )

    [3] => Array
        (
            [title] => Two
            [num] => 2
        )

)

After:

Array
(
    [0] => Array
        (
            [title] => One
            [num] => 1
        )

    [1] => Array
        (
            [title] => Two
            [num] => 2
        )

    [2] => Array
        (
            [title] => Tree
            [num] => 3
        )

    [3] => Array
        (
            [title] => Four
            [num] => 4
        )

)

But the problem is if the num is float or double format? Like this:

$arr = [
	[
		'title' => 'Tree',
        'num' => 0.3
	],
	[
		'title' => 'Four',
        'num' => 0.4
	],
	[
		'title' => 'One',
        'num' => 0.1
	],
	[
		'title' => 'Two',
        'num' => 0.2
	]
];
echo "Before: <pre>";
print_r($arr);
echo "</pre>";

usort($arr, function($a, $b) {
    return $a['num'] - $b['num'];
});

echo "After: <pre>";
print_r($arr);
echo "</pre>";

And run the code, and we can get the problem because the result is not in order.

Before:

Array
(
    [0] => Array
        (
            [title] => Tree
            [num] => 0.3
        )

    [1] => Array
        (
            [title] => Four
            [num] => 0.4
        )

    [2] => Array
        (
            [title] => One
            [num] => 0.1
        )

    [3] => Array
        (
            [title] => Two
            [num] => 0.2
        )

)

After:

Array
(
    [0] => Array
        (
            [title] => Two
            [num] => 0.2
        )

    [1] => Array
        (
            [title] => One
            [num] => 0.1
        )

    [2] => Array
        (
            [title] => Four
            [num] => 0.4
        )

    [3] => Array
        (
            [title] => Tree
            [num] => 0.3
        )

)

So to fix this, we can use PHP less than or equals operator, so change the code to be like this:

$arr = [
	[
		'title' => 'Tree',
        'num' => 0.3
	],
	[
		'title' => 'Four',
        'num' => 0.4
	],
	[
		'title' => 'One',
        'num' => 0.1
	],
	[
		'title' => 'Two',
        'num' => 0.2
	]
];
echo "Before: <pre>";
print_r($arr);
echo "</pre>";

usort($arr, function($a, $b) {
	return ($a['num'] <= $b['num']) ? -1 : 1;
});

echo "After: <pre>";
print_r($arr);
echo "</pre>";

And now run the code, so I right, we can get result like this:

Before:

Array
(
    [0] => Array
        (
            [title] => Tree
            [num] => 0.3
        )

    [1] => Array
        (
            [title] => Four
            [num] => 0.4
        )

    [2] => Array
        (
            [title] => One
            [num] => 0.1
        )

    [3] => Array
        (
            [title] => Two
            [num] => 0.2
        )

)

After:

Array
(
    [0] => Array
        (
            [title] => One
            [num] => 0.1
        )

    [1] => Array
        (
            [title] => Two
            [num] => 0.2
        )

    [2] => Array
        (
            [title] => Tree
            [num] => 0.3
        )

    [3] => Array
        (
            [title] => Four
            [num] => 0.4
        )

)

Explains:
So for line 23 is I create usort function with conditional if the a[‘num’] less than b[‘num’], the result must sort num value to lower than first index and if not dont sort. That function using ternary operator and using 1 for true and -1 for false.
Source:

  1. http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value
  2. http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php
  3. http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s