У меня есть два многомерных массива, и мне нужно отсортировать первый массив в том же порядке, что и второй массив, на основе разных ключей (но их значения одинаковы). В приведенном ниже примере мне нужно, чтобы $allOptions был отсортирован в том же порядке, что и $regOptions, но на основе значений clID == optID.
Однако не все подмассивы $allOptions (clID) присутствуют в подмассивах $regOptions (optID)....поэтому любые несовпадающие элементы в $allOptions будет брошен в низ/конец массива.
Как это сделать?
$allOptions = array(
array("clID"=> 171, ...other values),
array("clID"=> 191, ...other values),
array("clID"=> 131, ...other values),
array("clID"=> 101, ...other values),
array("clID"=> 201, ...other values),
array("clID"=> 181, ...other values),
...
array("clID"=> 99, ...other values), // not in regOptions
array("clID"=> 129, ...other values) // not in regOptions
array("clID"=> 139, ...other values)
) ;
$regOptions = array(
array("order"=>1,"optID"=> 131, ...other values),
array("order"=>2,"optID"=> 191, ...other values),
array("order"=>3,"optID"=> 181, ...other values),
array("order"=>4,"optID"=> 139, ...other values),
array("order"=>5,"optID"=> 101, ...other values),
array("order"=>6,"optID"=> 201, ...other values),
array("order"=>7,"optID"=> 171, ...other values)
...
) ;
Итак, результат будет следующим:
$allOptions = array(
array("clID"=> 131, ...other values),
array("clID"=> 191, ...other values),
array("clID"=> 181, ...other values),
array("clID"=> 139, ...other values)
array("clID"=> 101, ...other values),
array("clID"=> 201, ...other values),
array("clID"=> 171, ...other values),
...
array("clID"=> 99, ...other values), // not in regOptions
array("clID"=> 129, ...other values) // not in regOptions
) ;
Подробнее здесь: https://stackoverflow.com/questions/773 ... r-2d-array
Сортировка 2D-массива по порядку соответствующих значений в другом 2D-массиве ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение