PHP按键重新排列数组

时间:2014-06-11 17:16:57

标签: php arrays

我有以下数组...

Array (
    ["advertisers"] => Array (
        ...,
        ...,
        ...
    ),
    ["general"] => Array (
        ...,
        ...,
        ...
    ),
    ["publishers"] => Array (
        ...,
        ...,
        ...
    )
)

我想重新排列数组,以便"advertisers"排在第一位,"publishers"排在第二位,"general"排在最后。

2 个答案:

答案 0 :(得分:0)

这里:

<?
$fruits = Array( 
    "apples" => Array (
        "one",
        "two",
        "three"
    ),
    "oranges" => Array (
        "one",
        "two",
        "three"
    ),
    "bananas" => Array (
        "one",
        "two",
        "three"
    )
);
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

输出:

apples = Array 
bananas = Array  
oranges = Array

答案 1 :(得分:0)

你应该使用kso​​rt。它按数组键按字母顺序对数组进行排序。像这样的东西

<?php
 $arr = array('general'=>array(1,2,3), 'advertisers'=>array(7,8,9), 'publishers'=>array(11,12,13));
 ksort($arr);
 print '<pre>';
 print_r($arr);
 print '</pre>';
?>

<强>输出

Array
(
[advertisers] => Array
    (
        [0] => 7
        [1] => 8
        [2] => 9
    )

[general] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

[publishers] => Array
    (
        [0] => 11
        [1] => 12
        [2] => 13
    )

 )