如何解码存储在数据库中的数组的foreach值
How to decode foreach value of array stored in database
这是print_r($query->getResult())
:
Array ( [0] => stdClass Object ( [id] => 9
[user_id] =>
[title] =>
[message] => [{"title":"Nice","option":"Text 1"},{"title":"nice 2","option":"text 2"},{"title":"nice 3","option":"text 3"}]
[created_at] => 2020-06-21 16:59:49
)
)
我正在尝试使用以下方式回应它:
$query = $db->query("SELECT * from user_msg");
foreach (json_decode($query->getResult()) as $key => $additional_field) {
echo $additional_field->title;
}
但不幸的是我得到了这个错误:
json_decode() expects parameter 1 to be string, array given
我不知道 CodeIgnitor,但是根据结果数组和 JSON,你需要做这样的事情:
foreach ($query->getResult() as $row) {
foreach(json_decode($row->message) as $msg) {
echo $msg->title;
}
}
循环结果中的每一行,解码 message
并循环得到 title
s。
这是print_r($query->getResult())
:
Array ( [0] => stdClass Object ( [id] => 9
[user_id] =>
[title] =>
[message] => [{"title":"Nice","option":"Text 1"},{"title":"nice 2","option":"text 2"},{"title":"nice 3","option":"text 3"}]
[created_at] => 2020-06-21 16:59:49
)
)
我正在尝试使用以下方式回应它:
$query = $db->query("SELECT * from user_msg");
foreach (json_decode($query->getResult()) as $key => $additional_field) {
echo $additional_field->title;
}
但不幸的是我得到了这个错误:
json_decode() expects parameter 1 to be string, array given
我不知道 CodeIgnitor,但是根据结果数组和 JSON,你需要做这样的事情:
foreach ($query->getResult() as $row) {
foreach(json_decode($row->message) as $msg) {
echo $msg->title;
}
}
循环结果中的每一行,解码 message
并循环得到 title
s。