如何在codeigniter中访问数组内部的数组?

how to access the array inside the array in codeigniter?

我是 CI 的新手。我有一个功能可以访问数据列表作为类别。函数是这样的:

      public function get_category_tree()
{
    $this->ci->db->where('is_display','1');
    $query = $this->ci->db->get('product_categories');

    if ($query->num_rows() > 0) 
    {

        foreach($query->result() as $cat)
        {
            if($cat->parent_id=='0'){
                //category
                $categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => '');
            }else{
                //subcategory;
                $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
            }
        }
        return $categories_arr;
    }               
    return false;
}

这个函数是在通用库中定义的,我是这样从控制器访问它的:

    $this->data['category_tree'] = $this->general->get_category_tree();

这曾经给我这个结果:

    Array
  (
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] => 
    )
    Array
    (
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
    (
        [0] => Array
            (
                [id] => 78
                [parent_id] => 32
                [name] => Africa
            )

        [1] => Array
            (
                [id] => 79
                [parent_id] => 32
                [name] => Asia
            )

        [2] => Array
            (
                [id] => 80
                [parent_id] => 32
                [name] => Australia
            )

        [3] => Array
            (
                [id] => 81
                [parent_id] => 32
                [name] => Br Comm Other
            )

        [4] => Array
            (
                [id] => 82
                [parent_id] => 32
                [name] => Canada
            )

        [5] => Array
            (
                [id] => 83
                [parent_id] => 32
                [name] => Europe
            )

        [6] => Array
            (
                [id] => 84
                [parent_id] => 32
                [name] => Latin America
            )

        [7] => Array
            (
                [id] => 85
                [parent_id] => 32
                [name] => Middle East
            )

        [8] => Array
            (
                [id] => 86
                [parent_id] => 32
                [name] => Publications
            )

        [9] => Array
            (
                [id] => 87
                [parent_id] => 32
                [name] => Topical & Specialty
            )

        [10] => Array
            (
                [id] => 88
                [parent_id] => 32
                [name] => UK (Great Britain)
            )

        [11] => Array
            (
                [id] => 89
                [parent_id] => 32
                [name] => United States
            )

        [12] => Array
            (
                [id] => 90
                [parent_id] => 32
                [name] => Worldwide
            )

    )

   )

现在我将 php 升级到 7+,现在

    $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);

没有工作并给出了 php 错误,我只是删除了 [] 它变成了

     $categories_arr[$cat->parent_id]['subcat'] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);

现在当我 print_it 它给了我这个结果:

    Array
   (
  [id] => 19
  [parent_id] => 0
  [name] => DVDs & Movies
  [subcat] => 
   )
   Array
  (
  [id] => 32
  [parent_id] => 0
 [name] => Stamps
[subcat] => Array
    (
        [id] => 90
        [parent_id] => 32
        [name] => Worldwide
    )

  )

只有子数组的最后一个数组。我该如何解决。我搜索了很多有关升级和修复的信息,但找不到任何有用的东西。谁能帮忙。提前致谢

因为你在定义$categories_arr[$cat->id]时通过'subcat' => ''subcat定义为字符串。使用这个:

public function get_category_tree()
{
    $this->ci->db->where('is_display','1');
    $query = $this->ci->db->get('product_categories');

    if ($query->num_rows() > 0) 
    {

        foreach($query->result() as $cat)
        {
            if($cat->parent_id=='0'){
                //category
                $categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => array());
            }else{
                //subcategory;
                $categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
            }
        }
        return $categories_arr;
    }               
    return false;
}