Sort array of objects by object fields in PHP

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 data
class 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 object
function data2Object($data) {
    $class_object = new geekSchool($data);
    return $class_object;
}
  
// Comparator function used for comparator
// scores of two object/students
function comparator($object1, $object2) {
    return $object1->score > $object2->score;
}
  
// Generating array of objects
$school_data = array_map('data2Object', $gfg_array);
  
// Printing original object array data
print("Original object array:\n");
  
print_r($school_data);
  
// Sorting the class objects according 
// to their scores
usort($school_data, 'comparator');
  
// Printing sorted object array data
print("\nSorted object array:\n");
  
print_r($school_data);
  
?>

chevron_right


Output:

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
        )

)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.