Sort array of objects by object fields in PHP

04 Jan, 2019
Given an array of objects and the task is to sort the array by object by given fields.
Approach:The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field. Call usort() function with first argument as the array of objects and second argument as the comparator function on the basis of which comparison between two array objects has to be made.
Example:
<?php  // PHP program to show sorting of // array of objects by object fields  // School Data array$gfg_array = array(    array(        'score' => '100',        'name' => 'Sam',        'subject' => 'Data Structures'    ),    array(        'score' => '50',        'name' => 'Tanya',        'subject' => 'Advanced Algorithms'    ),    array(        'score' => '75',        'name' => 'Jack',        'subject' => 'Distributed Computing'    ));  // Class for encapsulating school dataclass geekSchool {          var $score, $name, $subject;      // Constructor for class initialization    public function geekSchool($data) {        $this->name = $data['name'];        $this->score = $data['score'];        $this->subject = $data['subject'];    }}  // Function to convert array data to class objectfunction data2Object($data) {    $class_object = new geekSchool($data);    return $class_object;}  // Comparator function used for comparator// scores of two object/studentsfunction comparator($object1, $object2) {    return $object1->score > $object2->score;}  // Generating array of objects$school_data = array_map('data2Object', $gfg_array);  // Printing original object array dataprint("Original object array:\n");  print_r($school_data);  // Sorting the class objects according // to their scoresusort($school_data, 'comparator');  // Printing sorted object array dataprint("\nSorted object array:\n");  print_r($school_data);  ?>
Original object array:
Array
(
    [0] => geekSchool Object
        (
            [score] => 100
            [name] => Sam
            [subject] => Data Structures
        )

    [1] => geekSchool Object
        (
            [score] => 50
            [name] => Tanya
            [subject] => Advanced Algorithms
        )

    [2] => geekSchool Object
        (
            [score] => 75
            [name] => Jack
            [subject] => Distributed Computing
        )

)

Sorted object array:
Array
(
    [0] => geekSchool Object
        (
            [score] => 50
            [name] => Tanya
            [subject] => Advanced Algorithms
        )

    [1] => geekSchool Object
        (
            [score] => 75
            [name] => Jack
            [subject] => Distributed Computing
        )

    [2] => geekSchool Object
        (
            [score] => 100
            [name] => Sam
            [subject] => Data Structures
        )

)

PHP-array
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
PHP in_array() Function
How to delete an array element based on key in PHP?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to execute PHP code using command line ?
How to delete an array element based on key in PHP?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to pop an alert message box using PHP ?