【问题标题】:Why is not working in Codeigniter this array为什么在 Codeigniter 这个数组中不起作用
【发布时间】:2021-12-24 14:37:44
【问题描述】:

我有这个数组:

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 和类别名称! 令人惊讶的是,当我在 codeigniter 中将此代码用作函数时,出现错误:

消息:试图获取非对象的属性“id”

文件名:products/Categories.php

行号:225

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

如果我删除它,我将无法查看主要类别,例如

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

谁能告诉我这是怎么回事?为什么在 php 中只有代码可以工作,但是当我在 codeigniter 上用作控制器中的函数时,我得到了帽子错误!

【问题讨论】:

  • 尝试获取非对象的属性“id”,这意味着基本上您正在尝试获取数组元素的值,如对象样式语法。示例:对于数组,它将是 $data['name'] & 对于对象,它将是 $data->name
  • 您的代码制表符具有误导性,会产生不必要的线宽。
  • 我们无法使用提供的详细信息生成事件。当您在 CodeIgniter 中运行代码时,我们不知道为什么 id 属性不可用。

标签: php arrays codeigniter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2017-11-10
    • 2015-03-18
    • 2011-02-18
    • 2019-10-28
    • 1970-01-01
    • 2022-12-03
    相关资源
    最近更新 更多