简体   繁体   English

PHP foreach循环仅返回第一项

[英]PHP foreach loop only returns the first item

I'm trying to develop an online shop and when I try to list all my basket products, if I do 我正在尝试建立网上商店,当我尝试列出所有购物篮产品时,如果我这样做

$basket = array(
    '6512bd43d9caa6e02c990b0a82652dca' =>
    array(
        'id' => '11',
        'quantity' => '51',
        'price' => '0.28',
        'stock' => '50',
        'name' => 'Tomato',
        'description' => 'u (140 gr aprox.) (1,99 â¬/Kg.)',
        'discount' => '0',
        'img' => '10.png',
        'unique_id' => '6512bd43d9caa6e02c990b0a82652dca',
        'total' => 14.28
    ),
    '72b32a1f754ba1c09b3695e0cb6cde7f' =>
    array(
        'id' => '57',
        'quantity' => '15',
        'price' => '2.70',
        'stock' => '15',
        'name' => 'fish ',
        'description' => 'tuna 500 gr',
        'discount' => '0',
        'img' => '57.png',
        'unique_id' => '72b32a1f754ba1c09b3695e0cb6cde7f',
        'total' => 40.5
    )
);

if($basket)
{
    echo "Basket:" . '<br/>';
    print_r($basket);
    echo '<br/>';
    echo "Products:" . '<br/>';

    foreach($basket as $product)
    { 
        print($product);
        echo '<br/';
    }
}

This is the returned 这是返回的

Basket:
Array
(
    [6512bd43d9caa6e02c990b0a82652dca] => Array
    (
        [id] => 11
        [quantity] => 51
        [price] => 0.28
        [stock] => 50
        [name] => Tomato
        [description] => u (140 gr aprox.) (1,99 â¬/Kg.)
        [discount] => 0
        [img] => 10.png
        [unique_id] => 6512bd43d9caa6e02c990b0a82652dca
        [total] => 14.28
    )
    [72b32a1f754ba1c09b3695e0cb6cde7f] => Array
    (
        [id] => 57
        [quantity] => 15
        [price] => 2.70
        [stock] => 15
        [name] => fish
        [description] => tuna 500 gr
        [discount] => 0
        [img] => 57.png
        [unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f
        [total] => 40.5
    )
)
Productos:
Array ( [id] => 11 [quantity] => 51 [price] => 0.28 [stock] => 50 [name] => Tomato [description] => u (140 gr aprox.) (1,99 â¬/Kg.) [discount] => 0 [img] => 10.png [unique_id] => 6512bd43d9caa6e02c990b0a82652dca [total] => 14.28 )
57 [quantity] => 15 [price] => 2.70 [stock] => 15 [name] => fish [description] => tuna 500 gr [discount] => 0 [img] => 57.png [unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f [total] => 40.5 )

But, if I do 但是,如果我这样做

if($basket)
{
    echo "Basket:" . '<br/>';
    print_r($basket);
    echo '<br/>';
    echo "Products:" . '<br/>';

    foreach($basket as $product)
    { 
        print($product["name");
        echo '<br/';
    }
}

The returned is: 返回的是:

Basket:
Array ( [6512bd43d9caa6e02c990b0a82652dca] => Array ( [id] => 11 [quantity] => 51 [price] => 0.28 [stock] => 50 [name] => Tomato [description] => u (140 gr aprox.) (1,99 â¬/Kg.) [discount] => 0 [img] => 10.png [unique_id] => 6512bd43d9caa6e02c990b0a82652dca [total] => 14.28 ) [72b32a1f754ba1c09b3695e0cb6cde7f] => Array ( [id] => 57 [quantity] => 15 [price] => 2.70 [stock] => 15 [name] => fish [description] => tuna 500 gr [discount] => 0 [img] => 57.png [unique_id] => 72b32a1f754ba1c09b3695e0cb6cde7f [total] => 40.5 ) )
Products:
Tomato

I don't understand why it only shows the first item of the basket. 我不明白为什么它只显示篮子的第一项。 What can I do to fix it? 我该如何解决?

Thanks! 谢谢!

You're already doing it right, except to close the bracket after <br/ . 您已经做对了,除了在<br/之后关闭括号。

It should be <br/> 应该是<br/>

You are doing correct. 您做对了。 But when you miss the close the <br/ tag then data rendered but not visible on your end. 但是,当您错过关闭<br/标签时,则会呈现已呈现但最终看不到的数据。


Products: 产品介绍:
10 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM