简体   繁体   English

从MySQL数据库中创建数组…如何在PHP中删除方括号和双引号?

[英]ouputing an array from MySQL db…how to remove square brackets and double quotes in PHP?

I am trying to retrieve an array from a mySQL db and print the output to the screen. 我正在尝试从mySQL数据库检索数组并将输出打印到屏幕。

The array is encoded prior to writing to the db with the following: 在写入数据库之前,使用以下代码对数组进行编码:

$itemsarray = json_encode($items);

It is then INSERTed into the db with the following query: 然后使用以下查询将其插入数据库:

mysqli_query($con, "INSERT INTO pages (email, date, items, title, url, expiry) VALUES ('$email', '$timestamp', '$itemsarray', '$title', '$new_url', '$expiry')")

When I retrieve the value for $itemsarray onto the screen, the output appears like this: ["forks","knives","spoons","plates","cups","mugs","napkins"] . 当我在屏幕上检索$ itemsarray的值时,输出看起来像这样: ["forks","knives","spoons","plates","cups","mugs","napkins"]

I'm familiar with PHP and relatively new to actually writing any MySQL queries so it took me a while to just get it written and returned, so please be easy on me here. 我对PHP很熟悉,并且对于实际编写任何MySQL查询还比较陌生,因此花了我一段时间才将其编写并返回,所以请在这里轻松一些。 :) :)

Do I need to write a function of some sort that strips out the " " and [ ], or do I need some further arguments within my JSON function? 我是否需要编写某种将“”和[]去除的函数,还是在JSON函数中需要其他参数?

Per @Mark Baker 's recommendation, this is what worked: 根据@Mark Ba​​ker的建议,这是有效的:

$items_list = $row['items'];
$list = json_decode($items_list);
$comma_separated = implode(", ", $list);
echo $comma_separated;

It outputs something like this: 它输出如下内容:

List Contents: forks, knives, spoons, plates, cups, mugs, napkins

Thanks Mark! 谢谢马克!

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

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