2015-10-29 67 views
0

我有數組是這樣的:PHP重撥鍵通過模板

$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'** 
    ) 
); 

回答

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-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) 
+0

非常感謝,它的工作原理 – zafirov

+0

太棒了!我很高興我幫助你。 –