我是新來的CI。我有一個功能來訪問數據列表作爲類別。該功能是這樣的:如何在codeigniter中訪問數組內部的數組?
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
)
)
只的最後一個數組子陣列。我該如何解決它。我搜索了很多關於升級和修復的內容,但無法提供任何有用的信息。任何人都可以請幫忙。在此先感謝
什麼錯誤? – JYoThI
@JYoThI 致命錯誤:未捕獲錯誤:[]運算符不支持E:\ xampp \ htdocs \ tshirt_design \ application \ libraries \ general.php中的字符串:209堆棧跟蹤:#0 –
您將subcat定義爲字符串。所以現在你在字符串so .it上使用數組語法會給出這個錯誤。將subcat定義爲數組。正如@Mohmad在下面提到的。 – JYoThI