2012-01-16 28 views
-2

數組我有一個這樣的數組:如何壓平PHP

Array 
(
    [0] => Array 
     (
      [Edisi] => Array 
       (
        [0] => 193|Oktober|2001| 
       ) 

      [Pengantar] => Array 
       (
        [0] => Halo!! 
       ) 

我想它壓扁到這一點:

Array 
(
    [Edisi]=>193|oktober|2001|, 
    [Pengantar=>Halo!! 
) 
+0

這已經問,看到http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php – pduersteler 2012-01-16 07:50:06

+1

有你有沒有試圖自己解決它?如果是這樣,你提出了什麼?請使用附加信息編輯您的問題。 – zrvan 2012-01-16 08:01:36

回答

0

你見過array_values()?如果沒有,試試這個:

$newArray = Array(
    'edisi' => $oldArray[0]['edisi'][0], 
    'Pengantar' => $oldArray[0]['Pengantar'][0] 
); 

沒有違法,但這並不複雜,之前已經幾次要求上這個網站。我們不是來做你的功課或爲你建立你的課程。請在你四處尋求幫助之前花更多時間思考你的問題。那麼,如果你真的需要幫助,請解釋你試過的是什麼以及爲什麼你有問題。

+0

我嘗試,但值** 193 | Oktober | 2001 | **和**暈!! !!沒有.. – 2012-01-16 08:15:17

0

如果這個數組不型變化(這意味着有永遠是第一位的水平,那麼你的索引,然後零索引你的價值觀),你可以做這樣的:

$array = array(
    array(
     'foo' => array([0] => 'hello'), 
     'foo2' => array([0] => 'hello2'), 
    ), 
    array(
     'wecanhandlemore' => array([0] => 'hello3'), 
     'wecanhandlemore2' => array([0] => 'hello4'), 
    ), 
); 
$target = array(); 
foreach ($array as $subarray) { 
    foreach ($subarray as $key => $valWithIndex) { 
     $target[$key] = $valWithIndex[0]; 
    } 
} 

應導致:

array(
    'foo' => 'hello', 
    'foo2' => 'hello2', 
    'wecanhandlemore' => 'hello3', 
    'wecanhandlemore2' => 'hello4', 
);