将嵌套数据从MySQL转换为嵌套数组

时间:2019-01-08 09:55:12

标签: php mysql codeigniter multidimensional-array

我有一个表格类别。这是模式:-

|----------------------------------------------------------|
|  id  |   title_en   |         uid         |   parent_id  |
|----------------------------------------------------------|
|   1  |    Food      |   ehek330pdldie827  |      0       |
|----------------------------------------------------------|
|   2  |   Cuisines   |  8393kdo02o923awi20 |      0       |
|----------------------------------------------------------|
|   3  |  Fast Food   |  ri29jnwu29823urns0 |      1       |
|----------------------------------------------------------|
|   4  |   British    | eo301ks8292ke9202ms |      2       |
|----------------------------------------------------------|
|   5  |   Chinese    |  yh39kj201203msik7e |      2       |
|----------------------------------------------------------|
|   6  |   Outdoor    |  hsuw8920slsl7729kj |      0       |
|----------------------------------------------------------|

根类别的parent_id为0,而子类别的parent_id为父母的ID。

我想从MySQL表中获取数据并将其转换为多维数组。

这是我的代码:-

public function create_array_tree($entity, $parent = 0, $visibility_for = null)
{
    $tree               = array(); 
    $children           = array();
    $currentLevelCount  = array(
                            'type'      => 'count',                                         // get the count
                            'table'     => $this->config->item($entity . '_table'),         // specify the table    
                            'select'    => 'id, uid, title_en, parent_id',                  // specify the field to fetch
                            'condition' => array(
                                                'parent_id' => $parent                      // condition: 'parent matches the id'
                                            )                                               // specify the condition
                        );  

    $currentLevelData   = array(
                            'type'      => 'all',                                           // get all the data
                            'table'     => $this->config->item($entity . '_table'),         // specify the table    
                            'select'    => 'id, uid, title_en, parent_id',                  // specify the field to fetch
                            'condition' => array(
                                                'parent_id' => $parent                      // condition: 'parent matches the id'
                                            )                                               // specify the condition
                        );  
    $countLevel         = $this->find($currentLevelCount);                  // get the count of the rows that match the conditions
    if($countLevel > 0)
    {

        $dataLevel    = $this->find($currentLevelData);
        foreach($dataLevel as $key => $dl)
        {
            if($visibility_for == Globals::COLLAPSIBLE_TREE)
            {
                $data['text']       = $dl['title_en'];
                $data['href']       = '#'.$dl['title_en'];
                $data['nodes']      = $this->create_array_tree($entity, $dl['id'], Globals::COLLAPSIBLE_TREE);
                $tree[]             = $data;
            }
            else
            {
                $data['id']         = $dl['id'];
                $data['title_en']   = $dl['title_en'];
                $data['uid']        = $dl['uid'];
                $data['parent_id']  = $dl['parent_id'];
                $data['children']   = $this->create_array_tree($entity, $dl['id']);
                $tree[]             = $data;
            }
        }
    }
    return $tree;
}

这给了我这样的结果:-

{
  "status": 1,
  "tree": [
    {
      "text": "Food",
      "href": "#Food",
      "nodes": [
        {
          "text": "Fast Food",
          "href": "#Fast Food",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Cuisines",
      "href": "#Cuisines",
      "nodes": [
        {
          "text": "British",
          "href": "#British",
          "nodes": [

          ]
        },
        {
          "text": "Chinese",
          "href": "#Chinese",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Outdoor",
      "href": "#Outdoor",
      "nodes": []
    }
  ]
}

现在,我想对代码进行一些更改。没有任何子类别的类别不应具有“ {nodes”键。为此,我对代码做了些微更改:-

if($visibility_for == Globals::COLLAPSIBLE_TREE)
 {
        $data['text']       = $dl['title_en'];
        $data['href']       = '#'.$dl['title_en'];
        $children           = $this->create_array_tree($entity, $dl['id'], Globals::COLLAPSIBLE_TREE);
        if(isset($children) && !empty($children))
        {
            $data['nodes']  = $children;
        }
        $tree[]             = $data;
 }

这给了我这个错误的数据:-

{
  "status": 1,
  "tree": [
    {
      "text": "Food",
      "href": "#Food",
      "nodes": [
        {
          "text": "Fast Food",
          "href": "#Fast Food",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Cuisines",
      "href": "#Cuisines",
      "nodes": [
        {
          "text": "British",
          "href": "#British",
          "nodes": [

          ]
        },
        {
          "text": "Chinese",
          "href": "#Chinese",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Outdoor",
      "href": "#Outdoor",
      "nodes": [
        {
          "text": "British",
          "href": "#British",
          "nodes": [

          ]
        },
        {
          "text": "Chinese",
          "href": "#Chinese",
          "nodes": [

          ]
        }
      ]
    }
  ]
}

“室外”节点包含“烹饪”的节点

但是,如果我这样编写代码:-

if($visibility_for == Globals::COLLAPSIBLE_TREE)
{
    $data['text']       = $dl['title_en'];
    $data['href']       = '#'.$dl['title_en'];
    $children           = $this->create_array_tree($entity, $dl['id'], Globals::COLLAPSIBLE_TREE);
    if(isset($children) && !empty($children))
    {
        $data['nodes']  = $children;
    }
    else
    {
        $data['nodes']  = '';
    }
    $tree[]             = $data;
}

数据如下:-

{
  "status": 1,
  "tree": [
    {
      "text": "Food",
      "href": "#Food",
      "nodes": [
        {
          "text": "Fast Food",
          "href": "#Fast Food",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Cuisines",
      "href": "#Cuisines",
      "nodes": [
        {
          "text": "British",
          "href": "#British",
          "nodes": [

          ]
        },
        {
          "text": "Chinese",
          "href": "#Chinese",
          "nodes": [

          ]
        }
      ]
    },
    {
      "text": "Outdoor",
      "href": "#Outdoor",
      "nodes": ""
    }
  ]
}

我无法从没有孩子的类别中删除 nodes 标记。我该如何解决?

0 个答案:

没有答案