Skip to content
Advertisement

sort data by id in cake php

where i want to sort it. following array..

$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'
    )
);

and i want array like

$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'
    )
);

notice that , i need sort by sortByFID wise if sortByFID id common then sort their id ascending order.

i tried like this in cake php

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

i am new in cake php, and i am still get struggle to solve it.

Advertisement

Answer

umm it seems to much complicated, well you can first sort id, then sort sortByFld it will work.

just like.

$id = Set::sort($tests, '{n}.id', 'asc');
$a = Set::sort($id, '{n}.sortByFld', 'asc');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement