2011-12-12 100 views
1

我有這種情況:PHP動態數組創建

一個數據庫表 'pageitems' 與

zone | text 
------------ 
ZONE1 text1 
ZONE2 text2 
ZONE3 text3 
ZONE3 text4 

和foreach循環這樣

foreach($pageitems as $items) { 
... 
} 

裏面我想獲得一個數組像這樣

Array 
(
    [ZONE1] => Array(
     [0] => text1 
    ) 

    [ZONE2] => Array 
    (
     [0] => text2 
    ) 

    [ZONE3] => Array 
    (
     [0] => text3, 
     [1] => text4 
    ) 

) 

如何使用ob這個嗎? 感謝

回答

11

假設你不需要 「text3」 中

$arr = array(); 
foreach($pageitems as $items) { 
    $arr[$items['zone']][] = $items['text']; 
} 
之後逗號