为什么不在Codeigniter这个数组中工作



我有这个数组:

stdClass Object
(
[categories] => Array
(
[0] => stdClass Object
(
[id] => 330
[label] => Electrocasnice Mici
[children] => Array
(
[0] => stdClass Object
(
[id] => 335
[label] => Aspiratoare
)
[1] => stdClass Object
(
[id] => 374
[label] => Aparate pentru bucatarie
[children] => Array
(
[0] => stdClass Object
(
[id] => 373
[label] => Masini de tocat
[children] => Array
(
[0] => stdClass Object
(
[id] => 1153
[label] => Accesorii masini de tocat
)
)
)
)
)
)
)
[1] => stdClass Object
(
[id] => 698
[label] => Auto & RCA
[children] => Array
(
[0] => stdClass Object
(
[id] => 1136
[label] => Reparatii si echipamente auto
[children] => Array
(
[0] => stdClass Object
(
[id] => 909
[label] => Compresoare Redresoare and Accesorii
)
)
)
)
)
......................................

在php页面中,我测试了以下内容:

$object = new ProductsList();
$cats = $object->getCategories();
$cat = array();
foreach ($cats as $item) {
foreach ($item as $key => $value) {                 
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
}
}
}
}
}
}
}
}
}
}

输出为:

Array
(
[0] => Array
(
[id] => 330
[name] => Electrocasnice Mici
)
[1] => Array
(
[id] => 335
[name] => Aspiratoare
)
[2] => Array
(
[id] => 374
[name] => Aparate pentru bucatarie
)
[3] => Array
(
[id] => 373
[name] => Masini de tocat
)
[4] => Array
(
[id] => 1153
[name] => Accesorii masini de tocat
)
[5] => Array
(
[id] => 698
[name] => Auto & RCA
)
......................................................

正是我所需要的,从所有类别树中提取ID和Catgory名称!令人惊讶的是,当我在codeigniter中使用此代码作为函数时,我得到了错误:

消息:正在尝试获取非对象的属性"id">

文件名:products/Categories.php

行号:225

这一行是我php代码中的第一个$cat[] = array("id" => $value->id, "name" => $value->label);

如果我删除它,我就无法查看等主要类别

  • [label]=>Electrocasnice Mici
  • [label]=>自动&RCA

有人能告诉我这是怎么回事吗?为什么在php中只有代码可以工作,但当我在控制器中使用codeigniter作为函数时,我会出现错误!

@SoftBeko,用下面的代码替换foreach循环,您将在codeigniter 中获得预期结果

foreach ($cats as $items) {             
$cat[] = array("id" => $items->id, "name" => $items->label);
if(isset($items->children)){
foreach ($items->children as $key => $value) {  
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
if(isset($value->children)){
foreach ($value->children as $key => $value) {
$cat[] = array("id" => $value->id, "name" => $value->label);
}
}
}
}
}
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新