在空的foreach中循环后更改数组值

时间:2019-05-02 10:47:29

标签: php arrays loops foreach

我有一个通过SQL查询创建的数组 $ export ,后来对其进行了格式化。数组现在看起来像这样(这些是整个数组的最后2个数组)

    [6136] => Array
        (
            [id_product] => 247
            [product_name] => Verte Home - vzor B.7.
            [id_attribute] => 181
            [attribute_name] => Premium - Drážka - Tloušťka J (260-280mm)
            [price] => Array
                (
                    [0] => Array
                        (
                            ["id_attribute"] => 181
                            ["id_custo_group"] => "1"
                            ["price_impact"] => 4099.173554
                        )

                    [1] => Array
                        (
                            ["id_attribute"] => 181
                            ["id_custo_group"] => "2"
                            ["price_impact"] => 4099.173554
                        )

                    [2] => Array
                        (
                            ["id_attribute"] => 181
                            ["id_custo_group"] => "3"
                            ["price_impact"] => 4099.173554
                        )

                )

        )

    [6137] => Array
        (
            [id_product] => 247
            [product_name] => Verte Home - vzor B.7.
            [id_attribute] => 182
            [attribute_name] => Premium - Drážka - Tloušťka K (280-300mm)
            [price] => Array
                (
                    [0] => Array
                        (
                            ["id_attribute"] => 182
                            ["id_custo_group"] => "1"
                            ["price_impact"] => 4099.173554
                        )

                    [1] => Array
                        (
                            ["id_attribute"] => 182
                            ["id_custo_group"] => "2"
                            ["price_impact"] => 4099.173554
                        )

                    [2] => Array
                        (
                            ["id_attribute"] => 182
                            ["id_custo_group"] => "3"
                            ["price_impact"] => 4099.173554
                        )

                )

        )

但是,如果我现在将其放入此foreach循环中,并只打印嵌套的 price 数组

foreach ($export as $attribute){
        echo '<pre>';
        echo print_r($attribute['price']);
        echo '</pre>';
}

我得到了(同样,整个数组的最后两个数组)

Array
(
    [0] => Array
        (
            ["id_attribute"] => 181
            ["id_custo_group"] => "1"
            ["price_impact"] => 4099.173554
        )

    [1] => Array
        (
            ["id_attribute"] => 181
            ["id_custo_group"] => "2"
            ["price_impact"] => 4099.173554
        )

    [2] => Array
        (
            ["id_attribute"] => 181
            ["id_custo_group"] => "3"
            ["price_impact"] => 4099.173554
        )

)
1
Array
(
    [0] => Array
        (
            ["id_attribute"] => 182
            ["id_custo_group"] => "1"
            ["price_impact"] => 4099.173554
        )

    [1] => Array
        (
            ["id_attribute"] => 182
            ["id_custo_group"] => "2"
            ["price_impact"] => 4099.173554
        )

    [2] => Array
        (
            ["id_attribute"] => 181
            ["id_custo_group"] => "3"
            ["price_impact"] => 4099.173554
        )

)
1

请注意,最后一个数组中的id_attribute与源$ export数组中的id_attribute不同。仅在整个数组的最后一个嵌套数组中会发生这种情况。

我不知道为什么会这样,所以任何帮助将不胜感激。

0 个答案:

没有答案