PHP使用键进行多维数组排序

时间:2016-02-03 05:39:45

标签: php sorting multidimensional-array associative-array

我有一个如下所示的数组

Array
(

[1236669] => Array
    (
        [banner_type] => OMAX
        [bannerid] => 1236669
        [zone_id] => 119789
        [zone_name] => OMAX-Supply - Mobile Web Tablet Interstitial
        [zone_manager_id] => 76
        [zone_manager_name] => Thumbtribe Manager
        [banner_name] => THUMBTRIBE_MW_TAB_Interstitial
        [banner_manager_id] => 288
        [banner_manager_name] => OMAX Manager
        [prev_day_req] => 0
    )
)

我想在二级密钥的基础上对二维数组进行排序,这意味着我希望数组为:

Array
(

[1236669] => Array
    (
        [banner_type] => OMAX
        [bannerid] => 1236669
        [banner_name] => THUMBTRIBE_MW_TAB_Interstitial
        [banner_manager_id] => 288
        [banner_manager_name] => OMAX Manager
        [prev_day_req] => 0
        [zone_id] => 119789
        [zone_name] => OMAX-Supply - Mobile Web Tablet Interstitial
        [zone_manager_id] => 76
        [zone_manager_name] => Thumbtribe Manager

    )
)

我尝试了asort,ksort,array_multisort但没有任何工作

3 个答案:

答案 0 :(得分:1)

您必须在主阵列的每个子节点上使用kso​​rt。像这样...

<?php
$array =array ( 1236 => array
        (
            'banner_type' => 'OMAX',
            'bannerid' => 12369,
            'zone_id' => 1197,
            'zone_name' => 'OMAX-Supply - Mobile Web Tablet Interstitial',
            'zone_manager_id' => 76,

        )
    );

foreach ($array as $key => $arr){
    ksort($arr);
    $array[$key] = $arr;
}
echo '<pre>';
    print_r($array);
?>

答案 1 :(得分:0)

oneliner解决方案:

imageLoader.displayImage("file://" + YOUR_URI_HERE, YOUR_IMAGEVIEW, options, null);

array_mapphp遍历所有关联数组,并使用ksortphp对其键进行排序。

答案 2 :(得分:-1)

亲爱的,请尝试使用此代码 如果你的数组比这更像是

     <?php
        $marks =  Array
(

1236669 => Array
    (
        'banner_type' => 'OMAX',
        'bannerid' => '1236669',
        'zone_id' => '119789',
        'zone_name' => 'OMAX-Supply - Mobile Web Tablet Interstitial',
        'zone_manager_id' => '76',
        'zone_manager_name' => 'Thumbtribe Manager',
        'banner_name' => 'THUMBTRIBE_MW_TAB_Interstitial',
        'banner_manager_id' => '288',
        'banner_manager_name' => 'OMAX Manager',
        'prev_day_req' => '0'
    )
);


ksort($marks);
echo "<pre>";
print_r($marks);
echo "</pre>";
?> 

<强>输出

Array
(
      [1236669] => Array
      (
         [banner_type] => OMAX
         [bannerid] => 1236669
         [zone_id] => 119789
         [zone_name] => OMAX-Supply - Mobile Web Tablet Interstitial
         [zone_manager_id] => 76
         [zone_manager_name] => Thumbtribe Manager
         [banner_name] => THUMBTRIBE_MW_TAB_Interstitial
         [banner_manager_id] => 288
         [banner_manager_name] => OMAX Manager
         [prev_day_req] => 0
     )
 )
祝你好运