cakephp 在cake php中按id排序数据

iqxoj9l9  于 2022-11-12  发布在  PHP
关注(0)|答案(2)|浏览(158)

在我想排序它的地方。下面的数组..

$a = array(
    (int) 152 => array(
        'id' => '152',
        'name' => 'Grade 3: Unit #1: erewtrewt',
        'sortByFld' => '103'
    ),
    (int) 168 => array(
        'id' => '168',
        'name' => 'Grade 3: Unit #20: ##Media## worksheet',
        'sortByFld' => '2003'
    ),
    (int) 94 => array(
        'id' => '94',
        'name' => 'Grade 3: Unit #1: test worksheet',
        'sortByFld' => '103'
    ),
    (int) 98 => array(
        'id' => '98',
        'name' => 'Grade 3: Unit #1: 1234',
        'sortByFld' => '103'
    ),
    (int) 183 => array(
        'id' => '183',
        'name' => 'Grade 3: Unit #1: match ddd',
        'sortByFld' => '103'
    )
);

和我想要的数组

$a = array(

    (int) 94 => array(
        'id' => '94',
        'name' => 'Grade 3: Unit #1: test worksheet',
        'sortByFld' => '103'
    ),

    (int) 98 => array(
        'id' => '98',
        'name' => 'Grade 3: Unit #1: 1234',
        'sortByFld' => '103'
    ),

    (int) 152 => array(
        'id' => '152',
        'name' => 'Grade 3: Unit #1: erewtrewt',
        'sortByFld' => '103'
    ),

    (int) 183 => array(
        'id' => '183',
        'name' => 'Grade 3: Unit #1: match ddd',
        'sortByFld' => '103'
    ),
    (int) 168 => array(
        'id' => '168',
        'name' => 'Grade 3: Unit #20: ##Media## worksheet',
        'sortByFld' => '2003'
    )
);

请注意,我需要按sortByFID进行排序,如果sortByFID id是公共的,则按其id升序排序。
我试过像这样在蛋糕php

$a = Set::sort($tests, '{n}.sortByFld', 'asc');
$a = Hash::combine($tests, '{n}.id',  '{n}.name');

我是一个新的蛋糕php,我还在努力解决它。

50pmv0ei

50pmv0ei1#

嗯,这看起来很复杂,你可以先对id排序,然后再对sortByFld排序,这样就可以了。
就像。

$id = Set::sort($tests, '{n}.id', 'asc');
$a = Set::sort($id, '{n}.sortByFld', 'asc');
wn9m85ua

wn9m85ua2#

你可以在cake php中做排序,比如:

Hash::sort($tests, '{n}.id', 'asc');

相关问题