• php:如何使用PHP排序, key为字母+数字的数组(多维数组)


    你还在为如何使用PHP排序字母+数字的数组而烦恼吗?
    当时我就震惊了

    今天有个小伙伴在群里问:
    如何将一个key为字母+数字的数组按升序排序呢?

    举个例子:

    $test = [
        'n1' => 22423,
        'n10' => 22423,
        'n2' => 'werwerw',
        'n3' => 22423,
        'n4' => 22423,
        'n5' => 22423,
        'n21' => 22423,
        'n11' => 22423,
    ];
    

      

    希望将它排序成这样子:

    $test = [
        'n1' => 22423,
        'n2' => 'werwerw',
        'n3' => 22423,
        'n4' => 22423,
        'n5' => 22423,
        'n10' => 22423,
        'n11' => 22423,
        'n21' => 22423,
    ]
    

      

    PHP数组函数帮到你

    $test = [
        'n1' => 22423,
        'n10' => 22423,
        'n2' => 'werwerw',
        'n3' => 22423,
        'n4' => 22423,
        'n5' => 22423,
        'n21' => 22423,
        'n11' => 22423,
    ];
    ksort($test, SORT_NATURAL);
    var_dump($test);
    

      得到的结果:

    array (size=8)
      'n1' => int 22423
      'n2' => string 'werwerw' (length=7)
      'n3' => int 22423
      'n4' => int 22423
      'n5' => int 22423
      'n10' => int 22423
      'n11' => int 22423
      'n21' => int 22423
    

      

     https://www.cnblogs.com/ailhc/p/7594585.html

    另外还有常用的,针对二维数组进行的排序:

    $countRate = array(
      array(
        'rate'=>1,
    'name'=>'aabb'  
      ),
    array(
        'rate'=>2,
    'name'=>'bbcc'
    )
    )
    array_multisort(array_column($countRate, 'rate'), SORT_DESC, $countRate, array_column($countRate, 'dep'), SORT_DESC, $countRate);
  • 相关阅读:
    十月二十七学习报告
    十月二十六学习报告
    十月二十五学习报告
    十月二十四学习报告
    十月二十三学习报告
    十月二十二学习报告
    十月二十一学习报告
    十月十九学习报告
    十月十七学习报告
    十月十六学习报告
  • 原文地址:https://www.cnblogs.com/achengmu/p/7910077.html
Copyright © 2020-2023  润新知