首页 > 解决方案 > 如何对“列表”查找器查询进行排序?结果未按定义的顺序显示

问题描述

这些国家没有按升序排列。

$coutryTable = TableRegistry::get('Country');
$getall = $coutryTable
    ->find('list', [
        'keyField' => 'phonecode',
        'valueField' => function ($row) {
            return $row['name'] . ' (+ ' . $row['phonecode'] . ')';
        }
    ])
    ->order([
        'Country.name' => 'ASC'
    ]);

标签: cakephpcakephp-3.x

解决方案


尝试排序功能https://www.w3schools.com/php/func_array_asort.asp

$coutryTable = TableRegistry::get('Country');
//pr($coutryTable);die();
$getall = $coutryTable->find('list', ['keyField' => 'phonecode', 'valueField' => function ($row) {
            return $row['name'] . ' (+ ' . $row['phonecode'] . ')';
        }])
->toArray();

asort($getAll);

print_r($getAll);

推荐阅读