将分层数据嵌套到数组中

将分层数据嵌套到数组中

本文介绍了将分层数据嵌套到数组中-PHP(Codeigniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试解决此问题,但是有点困难.事实是,我试图将表中的数据按照它们相关的顺序嵌套到一个数组中.

I have been trying to solve this for while but its been a little difficult. the thing is, am trying nest the data from the table bellow into an array in the order by which they are related.

+-----+------+------------+
| uid | name | supermember|
+-----+------+------------+
|   1 | A    |      0     |
|   2 | B    |      1     |
|   3 | C    |      1     |
|   4 | D    |      2     |
|   5 | E    |      3     |
|   7 | G    |      3     |
|   9 | H    |      4     |
|  10 | I    |      4     |
|  11 | J    |      7     |
+-----+------+------------+

这是我所做的:

public function getDataAsBinaryTree($id)
    {
        $this->db->where('uid', $id);
        $query = $this->db->get('binary_tbl');
        $result = [];
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $k) {
                $result[$k->name] =  $this->tolevels($k->uid);
            }
         } else {
            $result = NULL;
         }
         return $result;
    }



    private function tolevels($id)
    {
        $this->db->where('supermember', $id);
        $query = $this->db->get('binary_tbl');
        $output = [];
        $count = $query->num_rows();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $key) {
                $output[$key->name] = (($count > 1) ? $this->tolevel($key->uid) : $this->tolevel($key->uid));
            }
        }
        return $output;
    }

预期输出


array(
        'A' => array(
                'B' => array(
                    'D' => array(
                       'H' => 'H',
                        'I' => 'I'
                       )
                     ),
                 'C' => array(
                     'E' => null,
                      'G' => array(
                         'J' => 'J'
                        )
                   )
                )
            );

我希望从这些方法返回的数据在键和值对中,但是当我在 getDataAsBinaryTree(1)方法上进行var_dump时,它返回仅包含键的数组.我认为问题出在递归上,但我只是不知道该如何解决.

i want the returned data from these methods to be in key and value pair but it instead returned an array with only keys when i var_dump on the getDataAsBinaryTree(1) method. I think the problem is from the recursion but i just don't know how to get around it.

结果

array (size=1)
  'A' =>
    array (size=2)
      'B' =>
        array (size=1)
          'D' =>
            array (size=2)
              ...
      'C' =>
        array (size=2)
          'E' =>
            array (size=0)
              ...
          'G' =>
            array (size=1)
              ...

推荐答案

老实说,我不完全理解您的问题,您应该提供输出代码的结构.但是正如您的函数 getDataAsBinaryTree 所建议的那样,您正在尝试创建一个二叉树,如果是这种情况,这就是要走的路.

Honestly, I don't understand your question completely, you should provide the structure of output code. But as your function getDataAsBinaryTree suggests you're trying to make a binary tree, if that's the case then this is the way to go.

public function getDataAsBinaryTree($id = false){

    // $this->db->where('uid', $id);
    $query  = $this->db->get('binary_tbl');
    $result = [];

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $k) {
            $result[$k->name] =  $this->tolevels($k->uid);
        }
    } else {
       $result = NULL;
    }
    // return $result;
    echo '<pre>'; print_r($result);
}

private function tolevels($id){

    $this->db->where('supermember', $id);

    $query  = $this->db->get('binary_tbl');
    $output = [];
    $count  = $query->num_rows();

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $key) {
            // $output[$key->name] = ($count > 0) ? $this->tolevels($key->uid) : $key->uid;
            $output[$key->name] = $key->uid;
            if($count > 0){ $this->tolevels($key->uid); };
        }
    }
    return $output;
}
Array
(
    [A] => Array
        (
            [B] => 2
            [C] => 3
        )

    [B] => Array
        (
            [D] => 4
        )

    [C] => Array
        (
            [E] => 5
            [G] => 6
        )

    [D] => Array
        (
            [H] => 7
            [I] => 8
        )

    [E] => Array
        (
        )

    [G] => Array
        (
        )

    [H] => Array
        (
            [J] => 9
        )

    [I] => Array
        (
        )

    [J] => Array
        (
        )

)

希望,这对您有帮助.:)

Hope, this helps you. :)

这篇关于将分层数据嵌套到数组中-PHP(Codeigniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:36