MYSQL亲子同一个表; PHP窝儿在父母多维阵 [英] MYSQL Parent Child Same Table; PHP Nest Children Within Parents as a Multidimensional-Array

查看:148
本文介绍了MYSQL亲子同一个表; PHP窝儿在父母多维阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MYSQL返回如下所示的阵列。我使用的列:id_parent自我参照表创建层次。所以为2的ID的条目可以与一个2'id_parent'的任何条目的父,等等。

 阵列
  (    [1] =>排列
        (
            [ID] => 2
            [名] =>关于
            [id_parent] =>空值
        )    [2] =>排列
        (
            [ID] => 4
            [名] =>关于儿童
            [id_parent] => 2
        )    [3] =>排列
        (
            [ID] =>五
            [名] =>关于孩子的孩子
            [id_parent] => 4
        )
  )

我怎么能窝孩子到一个数组其父阵列内

 阵列
  (    [1] =>排列
        (
            [ID] => 2
            [名] =>关于
            [id_parent] =>
            [儿童] =>排列
                      (
                          [ID] => 4
                          [名] =>关于儿童
                          [id_parent] => 2
                          [儿童] =>排列
                                    (
                                        [ID] =>五
                                        [名] =>关于孩子的孩子
                                        [id_parent] => 4
                                    )
                      )
        )
  )


解决方案

参考,以优势顺序并不重要(子节点可以将他们的parentnodes来之前):

  $树=阵列('NULL'=>阵列('孩子'=>阵()));
 的foreach($数组作为$项目){
    如果(使用isset($树[$项目['身份证'])){
       $树[$项目['身份证'] = array_merge($树[$项目['身份证'],$项目);
    }其他{
       $树[$项目['身份证'] = $项目;
    }    $ PARENTID = is_null($项目['id_parent'])? 'NULL':$项目['id_parent'];
    如果(使用isset($树[$ PARENTID])!)$树[$ PARENTID] =阵列('孩子'=>阵());
    //这个和放大器;就是魔法发生:任何改动$树[$项目['身份证']
    //将反映在项目$树[$ PARENTID] ['孩子们'],因为它们是相同的
    //变量。举例来说,增加一个孩子$树[$项目['身份证'] ['儿童]
    //将在可见
    // $树[$ PARENTID] ['孩子'] [<凡是指数$项目['身份证']有>] [子女]
    $树[$ PARENTID] ['孩子'] [] =&放大器; $树[$项目['身份证'];
 }
 $结果= $树['NULL'] ['孩子'];
 //始终未设置引用
 未设置($树);

MYSQL returns an array as shown below. I am using column: 'id_parent' to self reference the table to create hierarchy. So an entry with an 'id' of 2 can be the parent of any entry with a 'id_parent' of 2, and so on.

Array 
  (

    [1] => Array
        (
            [id] => 2
            [name] => About
            [id_parent] => NULL
        )

    [2] => Array
        (
            [id] => 4
            [name] => About Child
            [id_parent] => 2
        )

    [3] => Array
        (
            [id] => 5
            [name] => About Child's Child
            [id_parent] => 4
        )
  )

How can I nest the children into an array within their parent array

Array
  (

    [1] => Array
        (
            [id] => 2
            [name] => About
            [id_parent] => 
            [children] => Array
                      (
                          [id] => 4
                          [name] => About Child
                          [id_parent] => 2
                          [children] => Array
                                    (
                                        [id] => 5
                                        [name] => About Child's Child
                                        [id_parent] => 4
                                    )
                      )
        )
  )

解决方案

References, with the advantages that order doesn't matter (childnodes can come before their parentnodes):

 $tree = array('NULL' => array('children' => array()));
 foreach($array as $item){
    if(isset($tree[$item['id']])){
       $tree[$item['id']] = array_merge($tree[$item['id']],$item);
    } else {
       $tree[$item['id']] = $item;
    }

    $parentid = is_null($item['id_parent']) ? 'NULL' : $item['id_parent'];
    if(!isset($tree[$parentid])) $tree[$parentid] = array('children' => array());
    //this & is where the magic happens: any alteration to $tree[$item['id']
    //  will reflect in the item $tree[$parentid]['children'] as they are the same
    //  variable. For instance, adding a child to $tree[$item['id']]['children]
    //  will be seen in 
    //  $tree[$parentid]['children'][<whatever index $item['id'] has>]['children]
    $tree[$parentid]['children'][] = &$tree[$item['id']];
 }
 $result = $tree['NULL']['children'];
 //always unset references
 unset($tree);

这篇关于MYSQL亲子同一个表; PHP窝儿在父母多维阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆