2011-12-12 102 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']; 
} 
之后逗号