php - 使用 PHP 访问另一个对象内的数组内的对象

标签 php mysql arrays database codeigniter-3

我是 CI 新手。使用 codeigniter,我尝试打印类别及其项目,如下所示:

类别 1 名称

  1. 第 1 项
  2. 第 2 项

类别 2 名称

  • 第 3 项

  • 第 4 项

  • 第 5 项

  • 等等。 我在 Controller 内构建了一个数组和子数组,如下所示:

    $data['categories'] = $this->publicpages_model->getcategories();
    foreach($data['categories'] as $category)
    {
        $data['categories'][$category->cID]= $this->publicpages_model->getitems($category->cID);
    }
    

    在 View 中,我尝试使用以下代码,但无法完成它以获得上面讨论的所需输出。

    foreach($categories AS $category)
    {
        echo '<h1>' . $category->name . '</h1>';
    
        //Missing code to print the items
    
        echo "<br>";
    }
    

    虽然打印了类别名称,但我也收到以下错误:

    Severity: Notice
    Message: Trying to get property of non-object
    

    print_r($categories) 给出以下结果:

    Array ( [0] => stdClass Object ( [cID] => 1 [name] => Breakfast ) [1] => Array ( [0] => stdClass Object ( [itemID] => 1 [name] => Aaloo Paratha [description] => descriptio 1 [menu] => 1 [price] => 22 [tax] => 20 [sStatus] => ) ) [2] => stdClass Object ( [cID] => 7 [name] => Fast Food ) [5] => Array ( [0] => stdClass Object ( [itemID] => 5 [name] => Missi Roti [description] => Hot and Crunchy [menu] => 5 [price] => 5 [tax] => 1 [sStatus] => 1 ) ) [7] => )
    

    vardump 给出以下输出:

    array(5) { [0]=> object(stdClass)#22 (2) { ["cID"]=> string(1) "1" ["name"]=> string(9) "Breakfast" } [1]=> array(1) { [0]=> object(stdClass)#25 (7) { ["itemID"]=> string(1) "1" ["name"]=> string(13) "Aaloo Paratha" ["description"]=> string(12) "descriptio 1" ["menu"]=> string(1) "1" ["price"]=> string(2) "22" ["tax"]=> string(2) "20" ["sStatus"]=> string(0) "" } } [2]=> object(stdClass)#24 (2) { ["cID"]=> string(1) "7" ["name"]=> string(9) "Fast Food" } [5]=> array(1) { [0]=> object(stdClass)#26 (7) { ["itemID"]=> string(1) "5" ["name"]=> string(10) "Missi Roti" ["description"]=> string(15) "Hot and Crunchy" ["menu"]=> string(1) "5" ["price"]=> string(1) "5" ["tax"]=> string(1) "1" ["sStatus"]=> string(1) "1" } } [7]=> bool(false) }
    

    请帮忙解决。

    最佳答案

    你的代码有一个小问题。你想做的事情很好,但如何做才是问题。

    在此 foreach 循环中,您正在修改循环的同一数组;因此它破坏了 $data['categories'] 的结构阵列完全。因此,数组的第二个元素没有 name键,因此它会抛出该警告。

    foreach($data['categories'] as $category)
    {
        $data['categories'][$category->cID] = ...;
    }
    

    如果您尝试获取每个类别的子项,并将它们添加为新键,则需要修改 $category大批。不是外部数组:)所以像这样改变它。

    foreach($data['categories'] as $category)
    {
        $category->subItems = $this->publicpages_model->getitems($category->cID);
    }
    

    或者如果您想要 $category->cID而不是 key subItems ,您可以将上面的内容更改为:

    foreach($data['categories'] as $category)
    {
        $category->{$category->cID} = $this->publicpages_model->getitems($category->cID);
    }
    

    我希望它能有所帮助:)如果有任何不清楚的地方,请随时询问。

    关于php - 使用 PHP 访问另一个对象内的数组内的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920106/

    相关文章:

    php - 远程 MySQL 连接返回系统错误 : 111 or PDO error [2002]

    PHP - FOR 循环帮助

    c++ - 是否有一种代码密集度较低的方法来使用非默认构造函数初始化数组?

    php - 如何显示来自特定类别的一个特定 WP 帖子(提取的 ID)

    php - 这是 PHP 递归的废话吗?

    mysql - 尝试制作这些表格时遇到麻烦

    arrays - 如何以最有效的方式在大小为 100 的整数数组中找到 0,其中 99 个元素为 1,只有一个元素为 0

    php - 字段列表中的未知列。找不到哪里出了问题

    php - PHP/MySQL错误

    mysql - 将mysql从4.1.22升级到5.0