How to Sort a Multi-dimensional Array by Value

Sorting a multi-dimensional array by value is a common task in programming, especially when working with data that needs to be ordered or displayed in a specific order. In this article, we will explore different approaches to solving this problem in PHP.

Understanding the Problem

Let's first understand the problem at hand. We have a multi-dimensional array with each element containing three keys: hashtag, title, and order. We want to sort this array based on the value of the order key.

The input array looks like this:

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)

Solution Approach

In PHP, we can use the usort() function, which allows us to sort an array by a user-defined comparison function. In our case, we want to compare the order keys of each element to determine the order.

Let's see how we can implement this sorting algorithm.

Approach 1: Using an Anonymous Function

We can define an anonymous function (also known as a closure) to compare the order keys of two elements and sort the array accordingly. Here's an example:

function sortArrayByOrder($array) {
    usort($array, function($a, $b) {
        return $a['order'] - $b['order'];
    });
    return $array;
}

// Usage
$array = [
    [
        'hashtag' => 'a7e87329b5eab8578f4f1098a152d6f4',
        'title' => 'Flower',
        'order' => 3
    ],
    [
        'hashtag' => 'b24ce0cd392a5b0b8dedc66c25213594',
        'title' => 'Free',
        'order' => 2
    ],
    [
        'hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
        'title' => 'Ready',
        'order' => 1
    ]
];

$sortedArray = sortArrayByOrder($array);

// Output
print_r($sortedArray);

In the above example, we define a function sortArrayByOrder() which takes the array as a parameter. Inside this function, we call the usort() function and pass in an anonymous function as the comparison function.

The anonymous function compares the order keys of two elements, $a and $b, and returns a negative value if $a should be placed before $b, a positive value if $a should be placed after $b, and 0 if they are equal.

Finally, we call the sortArrayByOrder() function on our test array and print the sorted array using the print_r() function.

Approach 2: Using a Named Comparison Function

Alternatively, we can define a named function as our comparison function and pass it to the usort() function. Here's an example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

function sortArrayByOrder($array) {
    usort($array, 'sortByOrder');
    return $array;
}

// Usage
$array = [
    [
        'hashtag' => 'a7e87329b5eab8578f4f1098a152d6f4',
        'title' => 'Flower',
        'order' => 3
    ],
    [
        'hashtag' => 'b24ce0cd392a5b0b8dedc66c25213594',
        'title' => 'Free',
        'order' => 2
    ],
    [
        'hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
        'title' => 'Ready',
        'order' => 1
    ]
];

$sortedArray = sortArrayByOrder($array);

// Output
print_r($sortedArray);

In this example, we define a named function sortByOrder() to compare the order keys of two elements. The rest of the code is similar to the previous approach, where we define the sortArrayByOrder() function and pass the named function as the comparison function to usort().

Conclusion

In this article, we have explored different approaches to sorting a multi-dimensional array by value in PHP. We have discussed two methods: using an anonymous function and using a named comparison function.

Both methods utilize the usort() function, which allows us to sort an array based on a user-defined comparison function. By comparing the order keys of each element, we can arrange the array in the desired order.

Feel free to use the code snippets provided in this article as a starting point for sorting your own multi-dimensional arrays by value.