如何使用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

not tested, but you need to defined a structure and then you need to go recursive... so you can check where is the parent entry to my child ...

function structure($data){
    return array(
        'id' => $data['id'],
        'item_name' => $data['item_name'],
        'menu_type' => $data['menu_type'],
        'parent' => $data['parent'],
        'expanded' => false,
        'childs' => array()
    );
}

function recursiveImport(&$parent, $data){
    foreach($parent AS $key => $value){
        if($value['id'] == $data['parent']){
            $parent[$key]['childs'][] = structure($data);
            $parent[$key]['expanded'] = true;
            return true;
        }
        else{
            if(count($value['childs']) > 0){
                foreach($value['childs'] AS $key2 => $child){
                    $result = recursiveImport($parent[$key]['childs'][$key2], $data);
                    if($result === true) return true;
                }
            }
        }
    }
    return false;
}

$newarr = array();    
while($fetchparentMenu=mysql_fetch_array($selectparentMenu)) {
    $result = recursiveImport($newarr , $fetchparentMenu);
    if($result === false){
        $newarr[] = structure($fetchparentMenu);
    }
}

//output array

array ( 0 => array ( 'id' => '1', 'item_name' => 'Dashboard', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 1 => array ( 'id' => '2', 'item_name' => 'Admission', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 2 => array ( 'id' => '3', 'item_name' => 'Examination', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), ), 3 => array ( 'id' => '4', 'item_name' => 'CET', 'menu_type' => 'item', 'parent' => '0', 'expanded' => false, 'childs' => array ( ), )

Use following code

$m=0;
if(mysql_num_rows($selectparentMenu)>1) {

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