PHP:未识别的索引:productOptionValue

时间:2016-03-14 19:17:07

标签: php arrays multidimensional-array

foreach($optionValue['productOptionValue'] as $value): // error here

endforeach;

索引已存在。 不知道我做错了什么。

  Array
(
    [optionValues] => Array
        (
            [34] => Array
                (
                    [id_product_option] => 1
                    [option_value] => Hello
                    [required] => 1
                    [productOptionValue] => Array
                        (
                            [0] => Array
                                (
                                    [id_product_option_value] => 1
                                    [id_option_value] => 88
                                    [quantity] => 12
                                    [subtract] => 1
                                    [price] => 232.0000
                                    [price_prefix] => r
                                    [weight] => 23.00000000
                                    [weight_prefix] => k
                                )

                        )

                )

请帮助!!

1 个答案:

答案 0 :(得分:0)

据我所知,你有一个带有“optionValues”属性的对象。让我们假设你的初始变量叫做“$ sexy”:

$sexy = Array
(
[optionValues] => Array
    (
        [34] => Array
            (
                [id_product_option] => 1
                [option_value] => Hello
                [required] => 1
                [productOptionValue] => Array
                    (
                        [0] => Array
                            (
                                [id_product_option_value] => 1
                                [id_option_value] => 88
                                [quantity] => 12
                                [subtract] => 1
                                [price] => 232.0000
                                [price_prefix] => r
                                [weight] => 23.00000000
                                [weight_prefix] => k
                            )

                    )

            )

所以以下应该可以解决问题:

foreach($sexy['optionValues'][34]['productOptionValue'] as $value):
    // I should be nice now and not throw an error here
endforeach;