我在数据库中插入json_encode()
的数据,现在我想得到(select * from <table> ...
)数据库只有name_unitsin
?我想输出这个 - &gt; 萨拉姆&amp; mokhles &amp;的 fadat
在数据库行units
中:
[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]},
{"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},
{"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]
。
$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$data = array();
foreach ($query_hotel_search->result() as $row)
{
$units = json_decode($row->units);
$data[] = array('name' => $row->name, 'units' =>$units['name_units']); // Line 24
}
echo json_encode($data);
这是代码输出:
遇到PHP错误
严重性: 注意
消息:未定义索引:name_units
行号: 24
[{“name”:“Jack”,“units”:null}]
答案 0 :(得分:1)
您从stdClass
得到json_decode
个对象的数组,而不是您期望的关联数组。看起来你的数据库中有一个JSON字符串数组。
假设您的数据库表是结构的,如果您想输出
salam &amp; mokhles &amp;的 fadat 强>
然后试试这个:
foreach( $query_hotel_search->result() as $row ) {
$units = json_decode( $row->units );
$names = '';
foreach( $units as $unit ) {
$names .= "{$unit->name_units} & ";
}
}
echo substr( $names, 0, -2 );