dsgo31121 2014-07-05 11:33
浏览 16
已采纳

为什么这个数组打印“数组”而不是值? (PHP)

I'm learning PHP and doing a basic eCommerce shopping cart for practice. I came across an issue that's stumped me. I made an array to display the current amount of a single item the shopper has in their cart, but rather than displaying the value it displays "array."

Below is what I'm using:

<?php
if(isset($_SESSION['Cart'])){

    $sql="SELECT * FROM products where Product_ID IN (";

    foreach($_SESSION['Cart'] as $id => $value){

        $sql.=$id.",";
        }
        $sql=substr($sql, 0, -1).") ORDER BY Category ASC";
        $query=mysql_query($sql);
        while($row=mysql_fetch_array($query)){
        ?>
            <p><?php echo $row['Name']?> x <?php echo $_SESSION['Cart'][$row['Product_ID']['quantity']] ?></p>
        <?php
        }
}else{

    echo "<p>There are no products selected</p>";
}
?>

I'm not sure if that's where the exact issue is though. The rest of my site is sourced here(Index) and here(Product page) if anyone would be so kind as to troubleshoot with me.

  • 写回答

2条回答 默认 最新

  • doulang6695 2014-07-05 18:39
    关注

    Based on your feedback if trying print_r($_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity]) gives you Array ( [quantity] => 1 )

    Then you can echo $_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity] to give you 1

    But i don't think this was the original intention. i think your real problem is from when you assign values to the array, there is a possible mismatching of keys.

    Try the code above and lets know if it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条