doudongfu8006 2018-03-05 10:57
浏览 118
已采纳

用逗号分隔的字符串变量中的数字排序

I have the following string

$ickkcids = "120,176,182,120,182,207,176,120,182,118,120,176";

I want to display it in a ascending ordered, like below.

118(1) //118 is the lowest integer so it comes first.
120(4)
176(3)
182(3)
207(1) //207 is the highest integer so it comes last.

This is what im doing

$array = explode(',', $ickkcids); 
        sort($array); 
        foreach($array as $arraye)
        {
            $html .= $arraye.'<br />';
        }

But this returns all numbers in ascending order and i don't know how to count in this.

Any idea how to achieve this?

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-03-05 11:08
    关注

    I support all above comments.First try your self, then ask a question.

    Any way @OITE OMAN, you can try this code for your question:

    $str = "120,176,182,120,182,207,176,120,182,118,120,176";
    $array = explode(",",$str);
    asort($array, SORT_NUMERIC);
    $latest_array = array_count_values($array);
    print_r($latest_array);
    foreach($latest_array as $key=>$value){
    echo $key."(".$value.")";
    echo "</br>"; 
    }
    

    Output:

    118(1)
    120(4)
    176(3)
    182(3)
    207(1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置