Home >Backend Development >PHP Tutorial >How to sort object array by object field in PHP? (code example)

How to sort object array by object field in PHP? (code example)

藏色散人
藏色散人Original
2019-03-19 11:08:348502browse

This article will introduce to you the implementation method of sorting object arrays by object fields in PHP.

How to sort object array by object field in PHP? (code example)

Given an array of objects, we sort the array by objects and given fields.

Method:

usort() function is a built-in function in PHP that is used to conditionally sort an array of elements using a given comparator function. The usort() function can also be used to sort an object array by object fields. When calling the usort() function, the first parameter is used as the object array, and the second parameter is used as the comparator function. The two array objects must be compared on this basis.

PHP code example:

<?php 
  
$gfg_array = array( 
    array( 
        &#39;score&#39; => &#39;100&#39;, 
        &#39;name&#39; => &#39;Sam&#39;, 
        &#39;subject&#39; => &#39;Data Structures&#39;
    ), 
    array( 
        &#39;score&#39; => &#39;50&#39;, 
        &#39;name&#39; => &#39;Tanya&#39;, 
        &#39;subject&#39; => &#39;Advanced Algorithms&#39;
    ), 
    array( 
        &#39;score&#39; => &#39;75&#39;, 
        &#39;name&#39; => &#39;Jack&#39;, 
        &#39;subject&#39; => &#39;Distributed Computing&#39;
    ) 
); 
  
class geekSchool {  
    var $score, $name, $subject; 
    
    public function geekSchool($data) { 
        $this->name = $data[&#39;name&#39;]; 
        $this->score = $data[&#39;score&#39;]; 
        $this->subject = $data[&#39;subject&#39;]; 
    } 
} 
  
function data2Object($data) { 
    $class_object = new geekSchool($data); 
    return $class_object; 
}  

function comparator($object1, $object2) { 
    return $object1->score > $object2->score; 
} 
  
$school_data = array_map(&#39;data2Object&#39;, $gfg_array); 
  
print("原始对象数组:\n"); 
  
print_r($school_data); 
  
usort($school_data, &#39;comparator&#39;); 
  
print("\n对象数组排序:\n"); 
  
print_r($school_data);

Output:

原始对象数组:
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
        )

)

对象数组排序:
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
        )

)

Related recommendations: "PHP Tutorial

This article is an introduction to the method of sorting object arrays by object fields in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to sort object array by object field in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn