PHP按对象字段对对象数组进行排序
给定一个对象数组,任务是按给定字段按对象对数组进行排序。
方法:
usort()函数是PHP的一个内置函数,用于使用给定的比较器函数有条件地对元素数组进行排序。 usort()函数还可用于按对象字段对对象数组进行排序。调用 usort()函数,第一个参数作为对象数组,第二个参数作为比较器函数,在此基础上必须进行两个数组对象之间的比较。
例子:
'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);
?>
输出:
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
)
)