PHP sort array alphabetically then numerically?

前端 未结 4 1776
忘掉有多难
忘掉有多难 2020-12-06 16:46

I have an array..

$test = array(\"def\", \"yz\", \"abc\", \"jkl\", \"123\", \"789\", \"stu\");

if I run sort() on it I get

4条回答
  •  借酒劲吻你
    2020-12-06 17:36

    In the following code is separate the data in two arrays: one is numerical the other is not and sort it and merge it.

    $arr1 = $arr2 = array();
    
    $foreach ($arr as $val) {
    
    if (is_numeric($val)) {array_push($arr2, $val); } 
    else {array_push($arr1, $val);}
    
    } 
    

    so you have to separate arrays whit numeric and non-numeric

    sort($arr2);
    sort($arr1);
    
    $test = array_merge($arr2,$arr1);
    

提交回复
热议问题