为什么这个PHP代码会提供此输出?

时间:2016-11-26 01:24:01

标签: php arrays iteration

我正在迭代一个数组;我的代码在第1行完整地打印数组,然后在下面的循环中打印每个单独的元素。如何生成数组应该无关紧要,并且在我将其打印出来的位置和逐个元素打印的位置之间没有代码。

第1行的输出是正确的;在循环生成的逐个元素的打印输出中,最后2个元素包含相同的数据,倒数第二个元素的数据被重复用于最后一个元素。

我的原始代码是在循环中进行一些额外的处理(而不是提供输出)。该代码正在做什么是无关紧要的,因为该代码被注释掉并且在我尝试调试此问题时已经被下面的简单代码所取代。

为什么输出会在循环中被修改?

以下是相关代码:

echo '<pre>'.print_r($flat, true).'</pre>';
foreach ($flat AS $tu_pid => $details) {
    echo '<pre>'.$tu_pid.': '.print_r($details, true).'</pre>';
}
die();

为清楚起见,这是最后一个元素,如第1行所示:

[TARG-E] => stdClass Object
(
    [stats] => stdClass Object
        (
            [id] => 1033
            [discount] => 0.00000
            [charged] => 49.90
            [charged_bd] => 49.90
            [fm_disc] => 0.00000
            [coupon_pct] => 0.00000
            [member_pct] => 0.00000
            [tu_sale] => 0.00000
            [tu_shelf] => 0.00000
            [tu_price] => 49.90
            [cart] => 49.9
            [list] => 49.9
            [regular] => 49.9
            [sale] => 
            [sale_pct] => 0.00000
            [sale_start] => 
            [sale_end] => 
            [list_all] => 0.00
            [reg_all] => 0.00
            [reg_tu] => 0.00
            [reg_child] => 0.00
            [price_tu] => 0.00
            [tu_child] => 0.00
            [nesting] => 0
            [cu_type] => 0
            [use_price] => 1
            [root] => 1
            [royalty_item] => -1
        )

    [lineage] => Array
        (
            [0] => Array
                (
                )

        )

    [owned] => 
    [products] => 
)

...并且由循环中的echo线表示:

TARG-E: stdClass Object
(
    [stats] => stdClass Object
        (
            [id] => 9745
            [discount] => 42.55975
            [charged] => 15.37
            [charged_bd] => 15.37
            [fm_disc] => 0.00000
            [coupon_pct] => 0.00000
            [member_pct] => 0.00000
            [tu_sale] => 32.95303
            [tu_shelf] => 0.00000
            [tu_price] => 15.37
            [cart] => 26.75174103
            [list] => 39.9
            [regular] => 39.9
            [sale] => 
            [sale_pct] => 0.00000
            [sale_start] => 
            [sale_end] => 
            [list_all] => 0.00
            [reg_all] => 0.00
            [reg_tu] => 0.00
            [reg_child] => 0.00
            [price_tu] => 0.00
            [tu_child] => 0.00
            [nesting] => 0
            [cu_type] => 0
            [use_price] => 1
            [root] => 
            [royalty_item] => 1019706
        )

    [lineage] => Array
        (
            [0] => Array
                (
                    [0] => PhotoGuide Set-6
                )

        )

    [owned] => 
    [products] => 
)

以下是相关代码的整个输出:

Array
(
    [PhotoGuide-Overview] => stdClass Object
        (
            [stats] => stdClass Object
                (
                    [id] => 9744
                    [discount] => 42.55975
                    [charged] => 15.37
                    [charged_bd] => 15.37
                    [fm_disc] => 0.00000
                    [coupon_pct] => 0.00000
                    [member_pct] => 0.00000
                    [tu_sale] => 32.95303
                    [tu_shelf] => 0.00000
                    [tu_price] => 15.37
                    [cart] => 26.75174103
                    [list] => 39.9
                    [regular] => 39.9
                    [sale] => 
                    [sale_pct] => 0.00000
                    [sale_start] => 
                    [sale_end] => 
                    [list_all] => 0.00
                    [reg_all] => 0.00
                    [reg_tu] => 0.00
                    [reg_child] => 0.00
                    [price_tu] => 0.00
                    [tu_child] => 0.00
                    [nesting] => 0
                    [cu_type] => 0
                    [use_price] => 1
                    [root] => 
                    [royalty_item] => 1019706
                )

            [lineage] => Array
                (
                    [0] => Array
                        (
                            [0] => PhotoGuide Set-6
                        )

                )

            [owned] => 
            [products] => 
        )

    [PhotoGuide-Turkey] => stdClass Object
        (
            [stats] => stdClass Object
                (
                    [id] => 9745
                    [discount] => 42.55975
                    [charged] => 15.37
                    [charged_bd] => 15.37
                    [fm_disc] => 0.00000
                    [coupon_pct] => 0.00000
                    [member_pct] => 0.00000
                    [tu_sale] => 32.95303
                    [tu_shelf] => 0.00000
                    [tu_price] => 15.37
                    [cart] => 26.75174103
                    [list] => 39.9
                    [regular] => 39.9
                    [sale] => 
                    [sale_pct] => 0.00000
                    [sale_start] => 
                    [sale_end] => 
                    [list_all] => 0.00
                    [reg_all] => 0.00
                    [reg_tu] => 0.00
                    [reg_child] => 0.00
                    [price_tu] => 0.00
                    [tu_child] => 0.00
                    [nesting] => 0
                    [cu_type] => 0
                    [use_price] => 1
                    [root] => 
                    [royalty_item] => 1019706
                )

            [lineage] => Array
                (
                    [0] => Array
                        (
                            [0] => PhotoGuide Set-6
                        )

                )

            [owned] => 
            [products] => 
        )

    [TARG-E] => stdClass Object
        (
            [stats] => stdClass Object
                (
                    [id] => 1033
                    [discount] => 0.00000
                    [charged] => 49.90
                    [charged_bd] => 49.90
                    [fm_disc] => 0.00000
                    [coupon_pct] => 0.00000
                    [member_pct] => 0.00000
                    [tu_sale] => 0.00000
                    [tu_shelf] => 0.00000
                    [tu_price] => 49.90
                    [cart] => 49.9
                    [list] => 49.9
                    [regular] => 49.9
                    [sale] => 
                    [sale_pct] => 0.00000
                    [sale_start] => 
                    [sale_end] => 
                    [list_all] => 0.00
                    [reg_all] => 0.00
                    [reg_tu] => 0.00
                    [reg_child] => 0.00
                    [price_tu] => 0.00
                    [tu_child] => 0.00
                    [nesting] => 0
                    [cu_type] => 0
                    [use_price] => 1
                    [root] => 1
                    [royalty_item] => -1
                )

            [lineage] => Array
                (
                    [0] => Array
                        (
                        )

                )

            [owned] => 
            [products] => 
        )

)

PhotoGuide-Overview: stdClass Object
(
    [stats] => stdClass Object
        (
            [id] => 9744
            [discount] => 42.55975
            [charged] => 15.37
            [charged_bd] => 15.37
            [fm_disc] => 0.00000
            [coupon_pct] => 0.00000
            [member_pct] => 0.00000
            [tu_sale] => 32.95303
            [tu_shelf] => 0.00000
            [tu_price] => 15.37
            [cart] => 26.75174103
            [list] => 39.9
            [regular] => 39.9
            [sale] => 
            [sale_pct] => 0.00000
            [sale_start] => 
            [sale_end] => 
            [list_all] => 0.00
            [reg_all] => 0.00
            [reg_tu] => 0.00
            [reg_child] => 0.00
            [price_tu] => 0.00
            [tu_child] => 0.00
            [nesting] => 0
            [cu_type] => 0
            [use_price] => 1
            [root] => 
            [royalty_item] => 1019706
        )

    [lineage] => Array
        (
            [0] => Array
                (
                    [0] => PhotoGuide Set-6
                )

        )

    [owned] => 
    [products] => 
)

PhotoGuide-Turkey: stdClass Object
(
    [stats] => stdClass Object
        (
            [id] => 9745
            [discount] => 42.55975
            [charged] => 15.37
            [charged_bd] => 15.37
            [fm_disc] => 0.00000
            [coupon_pct] => 0.00000
            [member_pct] => 0.00000
            [tu_sale] => 32.95303
            [tu_shelf] => 0.00000
            [tu_price] => 15.37
            [cart] => 26.75174103
            [list] => 39.9
            [regular] => 39.9
            [sale] => 
            [sale_pct] => 0.00000
            [sale_start] => 
            [sale_end] => 
            [list_all] => 0.00
            [reg_all] => 0.00
            [reg_tu] => 0.00
            [reg_child] => 0.00
            [price_tu] => 0.00
            [tu_child] => 0.00
            [nesting] => 0
            [cu_type] => 0
            [use_price] => 1
            [root] => 
            [royalty_item] => 1019706
        )

    [lineage] => Array
        (
            [0] => Array
                (
                    [0] => PhotoGuide Set-6
                )

        )

    [owned] => 
    [products] => 
)

TARG-E: stdClass Object
(
    [stats] => stdClass Object
        (
            [id] => 9745
            [discount] => 42.55975
            [charged] => 15.37
            [charged_bd] => 15.37
            [fm_disc] => 0.00000
            [coupon_pct] => 0.00000
            [member_pct] => 0.00000
            [tu_sale] => 32.95303
            [tu_shelf] => 0.00000
            [tu_price] => 15.37
            [cart] => 26.75174103
            [list] => 39.9
            [regular] => 39.9
            [sale] => 
            [sale_pct] => 0.00000
            [sale_start] => 
            [sale_end] => 
            [list_all] => 0.00
            [reg_all] => 0.00
            [reg_tu] => 0.00
            [reg_child] => 0.00
            [price_tu] => 0.00
            [tu_child] => 0.00
            [nesting] => 0
            [cu_type] => 0
            [use_price] => 1
            [root] => 
            [royalty_item] => 1019706
        )

    [lineage] => Array
        (
            [0] => Array
                (
                    [0] => PhotoGuide Set-6
                )

        )

    [owned] => 
    [products] => 
)

0 个答案:

没有答案