注意:循环时未定义的偏移量抛出一个带有foreach的对象数组

时间:2016-04-26 13:26:21

标签: php arrays loops object

我有以下对象数组:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [Sales] => 4
                )

            [1] => stdClass Object
                (
                    [Sales] => 6
                )

            [2] => stdClass Object
                (
                    [Sales] => 5
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [Sales] => 8
                )

            [1] => stdClass Object
                (
                    [Sales] => 7
                )

            [2] => stdClass Object
                (
                    [Sales] => 7
                )

        )

    [2] => Array
        (
            [0] => stdClass Object
                (
                    [Sales] => 5
                )

            [1] => stdClass Object
                (
                    [Sales] => 7
                )

            [2] => stdClass Object
                (
                    [Sales] => 6
                )

        )

    [3] => Array
        (
            [0] => stdClass Object
                (
                    [Sales] => 5
                )

            [1] => stdClass Object
                (
                    [Sales] => 6
                )

            [2] => stdClass Object
                (
                    [Sales] => 6
                )

        )

    [4] => Array
        (
            [0] => stdClass Object
                (
                    [Sales] => 4
                )

            [1] => stdClass Object
                (
                    [Sales] => 5
                )

            [2] => stdClass Object
                (
                    [Sales] => 11
                )

        )

)

我想获得每个对象的总销售额。所以我写了这段代码:

    public function getChannelsSales ($ChannelSales) {
        $totalChannelSales = array();
        $i = 0;
        foreach ($ChannelSales as $ChSales) {
            foreach ($ChSales as $value) {
                $totalChannelSales[$i] = $totalChannelSales [$i] + $value->Sales;
            }
            ++$i;
        }
        return $totalChannelSales;
}

运行代码后,我得到了正确答案:

Array
(
    [0] => 15
    [1] => 22
    [2] => 18
    [3] => 17
    [4] => 20
)

然而,我还得到了以下消息:

Notice: Undefined offset: 0 in ...\Sales.php on line 174
Notice: Undefined offset: 1 in ...\Sales.php on line 174
Notice: Undefined offset: 2 in ...\Sales.php on line 174
Notice: Undefined offset: 3 in ...\Sales.php on line 174
Notice: Undefined offset: 4 in ...\Sales.php on line 174

第174行就是这个:

$totalChannelSales[$i] = $totalChannelSales[$i] + $value->Sales;

即使答案是正确的,我也不知道问题在哪里(你可以自己总结一下)

任何想法?

修改

我通过添加两行

来获得解决方案
        $totalChannelSales[0] = 0;
  • 创建阵列之后和第一次foreach之前
  • 第一个foreach结束后,每两次之间的第二次

5 个答案:

答案 0 :(得分:1)

数组将从循环中逐个填充您的值。您无需添加它。

使用以下代码。

$totalChannelSales[$i] = $value->Sales;

答案 1 :(得分:0)

我想你的问题就在这里

$totalChannelSales[$i] = $totalChannelSales [$i] + $value->Sales;

您正在尝试设置甚至不存在的索引,您可以使用array_push,它将推送元素并迭代索引。

答案 2 :(得分:0)

我将引导您完成代码以找出错误:

foreach ($ChannelSales as $ChSales) {
        foreach ($ChSales as $value) {
            $totalChannelSales[$i] = $totalChannelSales [$i] + $value->Sales;
        }
        ++$i;
    }

现在在上面的代码中,我们说在$ i = 0时你处于第一个循环中,那时$totalChannelSales[$i] = $totalChannelSales [$i] + $value->Sales;将是$totalChannelSales[0] = $totalChannelSales [0] + $value->Sales;所以它将获取$ totalChannelSales [0]值在那一点上不存在同样适用于$ i的其他值。

答案 3 :(得分:0)

$totalChannelSales[$i] = $totalChannelSales [$i] + $value->Sales;

在上面的代码中,没有为右侧的$totalChannelSales [$i]定义值。你只需要下面的分配。

$totalChannelSales[$i] = $value->Sales;

答案 4 :(得分:0)

编辑:

我引入了一个临时变量$ tempsum,它将删除所有未定义的问题。

public function getChannelsSales ($ChannelSales) {
    $totalChannelSales = array();
    $i = 0;
    foreach ($ChannelSales as $ChSales) {
        $tempsum = 0;
        foreach ($ChSales as $value) {
            $tempsum += $value->Sales;
        }
       $totalChannelSales[$i] = $tempsum;
       $i++;
    }
   return $totalChannelSales;
}