Convert Associative Array in PHP and insert it to database

こ雲淡風輕ζ 提交于 2020-01-25 22:06:30

问题


I have a cart session stored in $_SESSION['cart'] that store the book's data (book_name, book_image, book_price, etc) if the user click 'buy'. I want to display the book_name only using this code :

foreach ($_SESSION['cart'] as $key => $value) {
    echo implode(', ', $key);
}

but it return into invalid arguments passed. I know that $key variable store the book name, and the $key variable store how much the book's amount. What i want is display the items into one string like this

book1, book2, book3

and then store it into one table row in my database like this :

     book_name     | total_price | etc..

----------------------------+-------------+------

book1, book2, book3 | 120000 | ....

So the conclusion of my problems above is :

  1. How to convert the book_name that is an associative array into one string?
  2. Then how to store the book_name (that has been 'converted' to one string) into one table row?

Anyone can help me? Thanks in advance :)

来源:https://stackoverflow.com/questions/41205252/convert-associative-array-in-php-and-insert-it-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!