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

时间:2017-06-14 09:28:41

标签: codeigniter php-7.1

我是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
    )

  )

仅子数组的最后一个数组。我该如何解决呢。我搜索了很多关于升级并修复它但却无法获得任何有用的东西。谁能请帮忙。提前致谢

1 个答案:

答案 0 :(得分:1)

因为您在定义subcat时将'subcat' => ''定义为$categories_arr[$cat->id]的字符串。使用此:

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;
}