Answer the question
In order to leave comments, you need to log in
How to sort an array by the values of multiple keys?
There is an array of objects. Let's say for a residential building:
[1] => array( 'Тип' => 'Квартира', "Номер объекта" => '4', 'ID' => '333'),
[2] => array( 'Тип' => 'Квартира', "Номер объекта" => '7', 'ID' => '444'),
[3] => array( 'Тип' => 'Машиноместо', "Номер объекта" => '3', 'ID' => '555' ),
[4] => array( 'Тип' => 'Машиноместо', "Номер объекта" => '2', 'ID' => '777' ),
[5] => array( 'Тип' => 'Квартира', "Номер объекта" => '2', 'ID' => '888'),
[5] => array( 'Тип' => 'Квартира', "Номер объекта" => '2', 'ID' => '888'),
[1] => array( 'Тип' => 'Квартира', "Номер объекта" => '4', 'ID' => '333'),
[2] => array( 'Тип' => 'Квартира', "Номер объекта" => '7', 'ID' => '444'),
[4] => array( 'Тип' => 'Машиноместо', "Номер объекта" => '2', 'ID' => '777' ),
[3] => array( 'Тип' => 'Машиноместо', "Номер объекта" => '3', 'ID' => '555' ),
Answer the question
In order to leave comments, you need to log in
Thanks for the multisort link, Vladislav Tishin .
Among the examples there I found the right solution:
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
$arObjects = array_orderby($arObjects, 'Тип', SORT_ASC, 'Номер объекта', SORT_ASC);
usort
php.net/manual/ru/function.usort.php
array_multisort
php.net/manual/ru/function.array-multisort.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question