dongliangkeng1056 2015-12-10 23:35
浏览 32
已采纳

仅使用特定键值对“内部”数组进行排序 - 使用uasort()?

Array opareas
(
[0] => Array
(
[name] = => array('opname1')

[subcats] => Array
    (
        [0] => Array
            (
                [amount] => 9883
                [perc] => 112.61394712853236
            )

        [1] => Array
            (
                [amount] => 1
                [perc] => 0.011394712853236098
            )

        [2] => Array
            (
                [amount] => 3679
                [perc] => 41.921148587055605
            )

    )

)

[1] => Array
(
[name] = => array('opname2')

[subcats] => Array
    (
        [0] => Array
            (
                [amount] => 12166
                [perc] => 56.8584380987989
            )

        [1] => Array
            (
                [amount] => 1473
                [perc] => 6.884142636818245
            )

        [2] => Array
            (
                [amount] => 955
                [perc] => 4.463242510632331
            )

        [3] => Array
            (
                [amount] => 6802
                [perc] => 31.789503201383372
            )

    )

)

PHP is excellent for sorting arrays, but I can't figure out what to use when having this issue.. Take a look of the array above.

I want to sort innner subcats[] array by the perc-key from lower to higher.

I want the result to be:

Array opareas
(
[0] => Array
(
[name] = => array('opname1')

[subcats] => Array
    (
        [0] => Array
            (
                [amount] => 1
                [perc] => 0.011394712853236098 //Lowest percentage value
            )

        [1] => Array
            (
                [amount] => 3679
                [perc] => 41.921148587055605
            )

        [2] => Array
            (
                [amount] => 9883
                [perc] => 112.61394712853236 //Highest percentage value
            )

    )

)

[1] => Array
(
[name] = => array('opname2');

[subcats] => Array
    (
        [0] => Array
            (
                [amount] => 955
                [perc] => 4.463242510632331 //Lowest percentage value
            )

        [1] => Array
            (
                [amount] => 1473
                [perc] => 6.884142636818245
            )

        [2] => Array
            (
                [amount] => 12166
                [perc] => 56.8584380987989
            )

        [3] => Array
            (
                [amount] => 6802
                [perc] => 31.789503201383372 //Highest percentage value
            )

    )

)

I've tried with this:

class whateverArray {

    private function sort_subcats_perc($a, $b) {               
        if ($a['subcats']['perc'] == $b['subcats']['perc'] ) {return 0;}
        return ( $a['subcats']['perc'] < $b['subcats']['perc'] ) ? -1 : 1;
    }

    public function showStuff() {
       $opareas = array(); //Given above

       //Sort subcat array from low to high based on perc-key
       uasort( $opareas, array( $this, 'sort_subcats_perc') );
    }

 );

}

I don't want the actual opareas[0] and operas[1] to switch position (is what happens when using the function I've tried) in the array, I only WANT the inner array subcats[] to change it's order based on perc-field/key. Is this possible with uasort? Or any other builtin of PHP excellent sorting functions?

  • 写回答

2条回答 默认 最新

  • dongsou0083 2015-12-10 23:53
    关注

    If you want to sort the sub-array, your comparison function needs to be a bit different. Then you can loop over the outer array by reference, so that the subcats will be sorted in place.

    foreach ($opareas as &$value) {
        usort($value['subcats'], function($a, $b) {
            if ($a['perc'] > $b['perc']) return 1;
            if ($a['perc'] < $b['perc']) return -1;
            return 0;
        });
    }
    unset($value); // unset the reference after looping
    

    You can use usort. You only need to use uasort if you care about the order of the keys in subcats.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题