【问题标题】:Accessing object inside array that is inside another object using PHP使用PHP访问另一个对象内的数组内的对象
【发布时间】:2017-12-21 07:39:17
【问题描述】:

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

第 1 类名称

  1. 项目 1
  2. 项目 2

第 2 类名称

  1. 第 3 项

  2. 第 4 项

  3. 第 5 项

等等。 我在控制器内部构建了一个数组和子数组,如下所示:

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

在视图中,我正在尝试使用以下代码,但我无法完成它以获得如上所述的所需输出。

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

请帮忙解决。

【问题讨论】:

  • 能否请您转储 $this->publicpages_model->getitems($category->cID) 的结果;并添加问题,以便我们更好地帮助您
  • 发布完整的错误信息并发布数据数组
  • @ChannaveerHakari 我已经更新了有问题的转储详细信息
  • @AbdullaNilam 我已经更新了有问题的转储详细信息
  • 检查你的foreach,有问题。您将$data['categories'] 更改为一个类别。但是您仍然使用 $data['categories'] 调用 foreach

标签: php mysql arrays database codeigniter-3


【解决方案1】:

您的代码中存在小问题。您尝试做的事情很好,但如何做才是问题。

在这个 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-&gt;cID 而不是密钥subItems,您可以将上面的内容更改为:

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

希望对您有所帮助:) 如果不清楚,请随时提出任何问题。

【讨论】:

    【解决方案2】:

    恕我直言,您的所作所为毫无意义 - 您应该将项目添加到类别中

    改用以下方法

    您的控制器

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

    你的看法

    foreach($categories AS $category)
    {
        echo '<h1>' . $category->name . '</h1>';
    
        if (isset($category->arrItems) && is_array($category->arrItems))
        {
            foreach($category->arrItems AS $objItem)
            {
                print_r($objItem);
            }
        }
    
        echo "<br>";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-28
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2020-08-15
      • 2017-07-01
      • 2016-02-02
      相关资源
      最近更新 更多