PHP按模板重新排序数组中的键

时间:2015-10-29 16:35:39

标签: php arrays sorting key

我有这样的数组:

$array = array(
            array(
                'Date' => '2015-10-23',
                'Week Day' => 'Friday',
                'Hour' => 'asdads',
                'Intoduction' => 'adsasdasdasd​',
                'Call Status' => 'asdadad:',
                'Call status options' => 'asdadsad',
                'Question 1' => 'asdadads',
                'Answer 1' => 'Psdado',
                'Question 2' => 'asdadadsasd',
                'Answer 2' => 'aasdadasdads',
                'Telephone 1' => 'asdadsadsqd',
                'Status tel 1' => 'asdasdad'
        ),
            array(
                'Date' => '2015-10-23',
                'Week Day' => 'Friday',
                'Hour' => 'asdads',
                'Intoduction' => 'asdadadsasdad',
                'Call Status' => "asdadsasdasd",
                'Call status options' => 'Busy',
                'Telephone 1' => 'asdadsasd',
                'Status tel 1' => 'sdasdasdad',
                'Question 1' => '',
                'Answer 1' => '',
                'Question 2' =>'',
                'Answer 2' =>''
            ) ...
);

随后的其他数组,键被混淆,但它们都具有相同的键。 我想用相同的顺序对所有数组键进行排序, 默认示例为$ array [0],下一个$ array [1],$ array [2],...所有按键都按$ array [0]排序。

如果输入是从顶部开始的数组,则结果应为:

$array = array(
    array(
        'Date' => '2015-10-23',
        'Week Day' => 'Friday',
        'Hour' => 'asdads',
        'Intoduction' => 'adsasdasdasd​',
        'Call Status' => 'asdadad:',
        'Call status options' => 'asdadsad',
        'Question 1' => 'asdadads',
        'Answer 1' => 'Psdado',
        'Question 2' => 'asdadadsasd',
        'Answer 2' => 'aasdadasdads',
        'Telephone 1' => 'asdadsadsqd',
        'Status tel 1' => 'asdasdad'
    ),
    array(
        'Date' => '2015-10-23',
        'Week Day' => 'Friday',
        'Hour' => 'asdads',
        'Intoduction' => 'asdadadsasdad',
        'Call Status' => "asdadsasdasd",
        'Call status options' => 'Busy',
        'Question 1' => '',
        'Answer 1' => '',
        'Question 2' =>'',
        'Answer 2' =>'',
        **'Telephone 1' => 'asdadsasd',
        'Status tel 1' => 'sdasdasdad'**
    )
);

1 个答案:

答案 0 :(得分:1)

我认为你可以使用这样的东西:

$array = array(
    array(
        'Date' => '2015-10-23',
        'Week Day' => 'Friday',
        'Hour' => 'asdads',
        'Intoduction' => 'adsasdasdasd​',
        'Call Status' => 'asdadad:',
        'Call status options' => 'asdadsad',
        'Question 1' => 'asdadads',
        'Answer 1' => 'Psdado',
        'Question 2' => 'asdadadsasd',
        'Answer 2' => 'aasdadasdads',
        'Telephone 1' => 'asdadsadsqd',
        'Status tel 1' => 'asdasdad'
    ),
    array(
        'Date' => '2015-10-25',
        'Week Day' => 'Monday',
        'Hour' => 'asdads',
        'Intoduction' => 'adsf',
        'Call Status' => "hgfv",
        'Call status options' => 'Hi',
        'Telephone 1' => 'nbvnb',
        'Status tel 1' => 'klasdfjdfjf',
        'Question 1' => '',
        'Answer 1' => '',
        'Question 2' =>'',
        'Answer 2' =>'5'
    )
);

$temp = array();
$keys = $array[0];
$sorted_keys = array();

foreach($keys as $key => $value) { //Choose the desired order depending on the first element keys
    $sorted_keys[] = $key;
}

foreach($array as $key => $value) { //For every element in the data array
    foreach($sorted_keys as $s_key => $s_value) { //Select to input the right key on the right place
        $temp[$key][$s_value] = $value[$s_value];
    }
}

var_dump($temp);

结果:

array (size=2)
  0 => 
    array (size=12)
      'Date' => string '2015-10-23' (length=10)
      'Week Day' => string 'Friday' (length=6)
      'Hour' => string 'asdads' (length=6)
      'Intoduction' => string 'adsasdasdasd​' (length=15)
      'Call Status' => string 'asdadad:' (length=8)
      'Call status options' => string 'asdadsad' (length=8)
      'Question 1' => string 'asdadads' (length=8)
      'Answer 1' => string 'Psdado' (length=6)
      'Question 2' => string 'asdadadsasd' (length=11)
      'Answer 2' => string 'aasdadasdads' (length=12)
      'Telephone 1' => string 'asdadsadsqd' (length=11)
      'Status tel 1' => string 'asdasdad' (length=8)
  1 => 
    array (size=12)
      'Date' => string '2015-10-25' (length=10)
      'Week Day' => string 'Monday' (length=6)
      'Hour' => string 'asdads' (length=6)
      'Intoduction' => string 'adsf' (length=4)
      'Call Status' => string 'hgfv' (length=4)
      'Call status options' => string 'Hi' (length=2)
      'Question 1' => string '' (length=0)
      'Answer 1' => string '' (length=0)
      'Question 2' => string '' (length=0)
      'Answer 2' => string '5' (length=1)
      'Telephone 1' => string 'nbvnb' (length=5)
      'Status tel 1' => string 'klasdfjdfjf' (length=11)