我尝试使用排序()来排序数组,但它失败 [英] I try to sort an array using sort() but it fails

查看:107
本文介绍了我尝试使用排序()来排序数组,但它失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 if ( $_GET['_value'] == 'moto'  )
     {
        $array[] = array('1' => 'Yamaha');
        $array[] = array('2' => 'Suzuki');  
        $array[] = array('3' => 'Triumph');
        $array[] = array('4' => 'KTM');
        $array[] = array('5' => 'Honda');
        $array[] = array('6' => 'Harley Davidson');
        $array[] = array('7' => 'Buell');
        $array[] = array('8' => 'MV Agusta');
        $array[] = array('9' => 'Ducati');
        $array[] = array('10' => 'Other');

      } 
 $array = sort($array);
 echo json_encode( $array );

这是code我已经和它的一个链接的下拉拉动。
我希望它返回按字母顺序排列但基于code你看到它返回一个空数组中的值。这可能是我在做错误/

that is the code i have and its pulled by a chained dropdown. I want it to return the values sorted alphabetically but based on the code you see it returns an empty array. what could be the mistake I am making /

推荐答案

这不是在所有的pretty但做这项工作。

It's not at all pretty but this does the job.

除非你的约束,否则你应该使用一些其他建议。

Unless you're constrained otherwise you should really use some of the other suggestions.

<?
if ( $_GET['_value'] == 'moto'  ) {
    $array[] = array('1' => 'Yamaha');
    $array[] = array('2' => 'Suzuki');  
    $array[] = array('3' => 'Triumph');
    $array[] = array('4' => 'KTM');
    $array[] = array('5' => 'Honda');
    $array[] = array('6' => 'Harley Davidson');
    $array[] = array('7' => 'Buell');
    $array[] = array('8' => 'MV Agusta');
    $array[] = array('9' => 'Ducati');
    $array[] = array('10' => 'Other');
    foreach($array as $i => $v)
    {
        $v = array_values($v);
        $sort[] = $v[0];
    }
    sort($sort);
    $c = 0;
    foreach($sort as $i => $v)
    {
        $c++;
        $sorted[] = array($c=>$v);
    }

    echo json_encode($sorted);
}
?>

这篇关于我尝试使用排序()来排序数组,但它失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆