如何使用php获取mysql数据以父子方式构建数组

this is my code where i am fetching my mysql record.

$parentChildArr=array();
//mysql query for fetching parent and child record
$selectparentMenu=mysql_query("SELECT `id`,`item_name`,`menu_type`,`parent` FROM `epic_master_menu`"); 

    if(mysql_num_rows($selectparentMenu)>1) {

while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
$parentChildArr[]=$fetchparentMenu['id'];
$parentChildArr[]=$fetchparentMenu['item_name'];
$parentChildArr[]=$fetchparentMenu['menu_type'];
$parentChildArr[]=$fetchparentMenu['parent'];
}

var_export($parentChildArr); // exporting or printing arrays

// when i export the array i get this output.

   array ( 0 => '1', 1 => 'Dashboard', 2 => 'item', 3 => '0',
 4 => '2', 5 => 'Admission', 6 => 'item', 7 => '0', 8 => '3', 
9 => 'Examination', 10 => 'item', 11 => '0', 12 => '4', 
13 => 'CET', 14 => 'item', 15 => '0');

but the problem is that i want to build the array like this.

  $newarr=array ( 'dataSource' => array ( 0 => array ( 'id' => '1', 
'text' => 'Dashboard', 'expanded' => 'true', 'spriteCssClass' => 'rootfolder', 
'items' => array ( 0 => array ( 'id' => '89', 'text' => 'Users', 
'expanded' => true, 'spriteCssClass' => 'folder', 
'items' => array ( 0 => array ( 'id' => '94', 'text' => 'Users',
 'spriteCssClass' => 'html', ), 1 => array ( 'id' => '94', 
'text' => 'Users', 'spriteCssClass' => 'html', ), 2 => array ( 
'id' => '94', 'text' => 'Users', 'spriteCssClass' => 'image' ) ) ) ) ) ));

database table view is... enter image description here

i am not getting the logic that how to build the array like $newarr. thank you

查看全部
doz15449
doz15449
2014/02/26 06:25
  • php
  • mysql
  • arrays
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复