0

I've and array, I want to sort this array ascending order by [sys_title] key index. What should I do?

[0] => Array
(
    [sys_id] => 9
    [sys_title] => Checklist
    [sys_home] => /cp/system/chl/
)

[1] => Array
(
    [sys_id] => 8
    [sys_title] => Bakery Ordering System
    [sys_home] => /cp/system/bos/
)

Expected Result should be like this:

[0] => Array
(
    [sys_id] => 8
    [sys_title] => Bakery Ordering System
    [sys_home] => /cp/system/bos/
)

[1] => Array
(
    [sys_id] => 9
    [sys_title] => Checklist
    [sys_home] => /cp/system/chl/
)
3
  • 4
    use usort() function Commented Apr 25, 2016 at 9:02
  • Can you please explain? Commented Apr 25, 2016 at 9:04
  • php.net/manual/en/function.usort.php Commented Apr 25, 2016 at 9:06

2 Answers 2

1

You can try this piece of code:

usort($data,function($a,$b){
    return strcmp($a['sys_title'],$b['sys_title']);
});

print_r($data);
Sign up to request clarification or add additional context in comments.

Comments

0

Suppose your array name is $a then:

$tmp = Array();
foreach($a as &$ma) {
    $tmp[] = &$ma["sys_title"];
    array_multisort($tmp, $a);
}

Result will be

Array
(
    [0] => Array
        (
            [sys_id] => 8
            [sys_title] => Bakery Ordering System
            [sys_home] => /cp/system/bos/
        )

    [1] => Array
        (
            [sys_id] => 9
            [sys_title] => Checklist
            [sys_home] => /cp/system/chl/
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.