对于循环通过php数组只显示最后一个值

时间:2015-07-10 07:21:59

标签: php arrays for-loop array-push

我有一个数组,我在header.php文件中只包含一次。

的header.php:

<?
session_start();
include_once("array.php");
?>

array.php:

<?$foo["cart"] = array();?>

order.php我有一个在提交表单时执行的数组和代码。在array_push之后,您将被重定向到webshop.php,您可以在其中查看购物卡。

order.php:

<? 
array_push($foo["cart"], array ('imagename'=>"$imagename",'size'=>"$size", 'price'=> "$price"));    $_SESSION["order"] = $foo;
?>

webshop.php:

<div class="shopping-card" > 
<? echo "<ol>";
for ($row = 0; $row < 3; $row++)
{
    echo "<li><b>Products:</b>";
    echo "<ul>";
    foreach( $_SESSION["order"]["cart"][$row] as $key => $value)
    {
        echo "<li>".$value."</li>";
    }
    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>
</div>

enter image description here

正如您在此处所见,它仅显示最后一个值。

我希望你们能帮助我。

1 个答案:

答案 0 :(得分:0)

你有这样的阵列:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [Main] => stdClass Object
                        (
                            [cart_main_id] => 1
                            [cart_main_device_id] => AAAAAAA
                            [cart_main_table_id] => 36
                            [cart_main_table_number] => 18
                            [tbl_type] => 2
                        )

                    [orders] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [cart_detail_id] => 1
                                    [cart_detail_main_id] => 1
                                    [cart_detail_device_id] => AAAAAAA
                                    [cart_detail_product_id] => 5
                                    [cart_detail_qty] => 1
                                    [cart_detail_price] => 13.75
                                    [product_name] => #?#en#?#Turnips pork ribs soup#?#en#?##?#zh#?#èåœæŽ'骨汤#?#zh#?#
                                )

                            [1] => stdClass Object
                                (
                                    [cart_detail_id] => 1
                                    [cart_detail_main_id] => 1
                                    [cart_detail_device_id] => AAAAAAA
                                    [cart_detail_product_id] => 5
                                    [cart_detail_qty] => 1
                                    [cart_detail_price] => 13.75
                                    [product_name] => #?#en#?#Tom Yum Pork Ribs#?#en#?##?#zh#?#冬è«æŽ'骨#?#zh#?#
                                )

                        )

                )


        )

)

然后U CAn从数组中提取正确的值

    $tble_cnt = 0;

if (count($finalarray))
    {
    $ttlrcrds = count($finalarray['data']);
    $b = 0;
    for ($i = 0; $i < $ttlrcrds; $i++)
        {
        echo "Table No : " . $finalarray['data'][$i]['Main']->cart_main_table_number;
        $total_brand_cnt = count($finalarray['data'][$i]['orders']);
        for ($j = 0; $j < $total_brand_cnt; $j++)
            {
            $numbers_new = $j + 1;
            echo extract_language_part($finalarray['data'][$i]['product'][$j]->product_name, $this->session->userdata('current_language_iso'));
            }

        $tble_cnt++;
        }
    }