Php - 按字母顺序排列

时间:2012-10-07 16:15:09

标签: php sql

我有一个PHP代码:

function display_categories_list($categories_list) {

    $display .= '<ul data-role="listview" data-theme="d" >';
    for($i=0; $i<count($categories_list); $i++) {
        $display .= '<li><a href="javascript:" class="displayStoresListByCategoryBtn" id="'.$categories_list[$i]['id'].'">';
        $display .= '<h3>'.$categories_list[$i]['name'].'</h3>';
        $display .= '<span class="ui-li-count"><font color="red"><small>'.$categories_list[$i]['nb'].'</small></font></span>';
        $display .= '</a></li>';
    }
    $display .= '</ul>';

    return $display;
}

我无法访问sql查询,我想用“names alphabeticals ASC”命令我的结果。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

$categories_list[0]['name'] = "Bob";
$categories_list[0]['id'] = 1;
$categories_list[1]['name'] = "Carol";
$categories_list[1]['id'] = 2;
$categories_list[2]['name'] = "Ted";
$categories_list[2]['id'] = 3;
$categories_list[3]['name'] = "Alice";
$categories_list[3]['id'] = 4;

// Obtain a list of columns
foreach ($categories_list as $key => $row) {
    $tmp[$key]  = $row['name'];
}

array_multisort($tmp, SORT_ASC, $categories_list);

print_r($categories_list);

给出:

Array
(
    [0] => Array
        (
            [name] => Alice
            [id] => 4
        )

    [1] => Array
        (
            [name] => Bob
            [id] => 1
        )

    [2] => Array
        (
            [name] => Carol
            [id] => 2
        )

    [3] => Array
        (
            [name] => Ted
            [id] => 3
        )

)

http://php.net/manual/en/function.array-multisort.php