我有一个数据库表,其中一列包含一个json值。我想知道我如何使用php mysql查询获取" title"的值。和"名称"来自"元素"列。
id | name | elements
---------------------
1 | Dan | {
"book": {
"0": { "title": "The Oblivion of Hope" }
},
"author": {
"0": { "name": "David Addoteye" },
"1": { "date": "2015-93-13"}
}
}
elements => book => 0 => title
elements => author=> 0 => name
如果有人可以帮我解决这个问题,我会很高兴的,谢谢你。
答案 0 :(得分:1)
从数据库中获取条目并使用json_decode()
将其转换为关联数组。
一些示例代码:
// Equivalent of obtained from DB query.
$fetched_value = '{"book":{"0":{"title":"The Oblivion of Hope"}},"author":{"0":{"name":"David Addoteye"},"1":{"date":"2015-93-13"}}}';
$decoded = json_decode($fetched_value, true);
echo $decoded['book'][0]['title'];
echo $decoded['author'][0]['name'];
答案 1 :(得分:0)