doz15449 2014-02-26 06:25
浏览 49
已采纳

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

  • 写回答

2条回答 默认 最新

  • duanbu1998 2014-02-26 06:58
    关注

    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 ( ), )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思