PHP - 不能使用标量作为数组警告 [英] PHP - cannot use a scalar as an array warning
本文介绍了PHP - 不能使用标量作为数组警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
$final = array();
foreach ($words as $word) {
$query = "SELECT Something";
$result = $this->_db->fetchAll($query, "%".$word."%");
foreach ($result as $row)
{
$id = $row['page_id'];
if (!empty($final[$id][0]))
{
$final[$id][0] = $final[$id][0]+3;
}
else
{
$final[$id][0] = 3;
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];
}
}
}
代码似乎工作正常,但我收到此警告:
The code SEEMS to work fine, but I get this warning:
Warning: Cannot use a scalar value as an array in line X, Y, Z (the line with: $final[$id][0] = 3, and the next 2).
谁能告诉我如何解决这个问题?
Can anyone tell me how to fix this?
推荐答案
在向数组中添加元素之前,您需要将$final[$id]
设置为数组.用其中之一初始化它
You need to set$final[$id]
to an array before adding elements to it. Intiialize it with either
$final[$id] = array();
$final[$id][0] = 3;
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];
或
$final[$id] = array(0 => 3);
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];
这篇关于PHP - 不能使用标量作为数组警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文