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条)

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成