我有一个多维数组,如下所示:
$rows[] = $row;
现在,我想通过循环此数组来创建变量。这是我尝试过的方法:
foreach ($rows as $k => $value) {
//echo '<pre>',print_r($value).'</pre>';
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
但是会产生如下错误:
......非法的字符串偏移量'news_id'
这是-echo '<pre>',print_r($value).'</pre>';
Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
1
Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
1
有人可以告诉我我做错了什么吗?
更新
echo '<pre>',print_r($rows).'</pre>';
Array
(
[0] =>
[1] => Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
[2] => Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
)
1
答案 0 :(得分:0)
使用isset
函数,因为您的0索引在$row
中为空
foreach ($rows as $k => $value) {
if(isset($value['news_id'])){
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
}
将数据分配给$rows
时应该添加检查(条件)