我在 PHP 中收到一个奇怪的警告,它不能将数组转换为字符串,这正是
implode()
语句的用途。
PHP 警告:第 60 行 C:\inetpub\wwwroot\Bible4All\simpleregexmatch.php 中的数组到字符串的转换
第 60 行 是包含内爆声明的行。
下面是我正在使用的测试代码,任何人都应该能够“按原样”成功复制/粘贴和运行代码,除非
implode()
语句未被注释掉。一旦 implode() statement is commented out, it runs up to the
print_r()` 语句具有所需的输出。
$debug=1;
$searchkeywords = array();
$searchkeywords[] = "jesus";
$searchkeywords[] = "christ";
$string = "And this is life eternal, that they might know thee the only true God, and Jesus Christ, whom thou hast sent.";
$array_of_bible_verse = preg_split("/\s+/", $string);
$outputverse[] = array();
foreach($array_of_bible_verse as $verse_by_word)
{
$tmpArray[] = array();
// Check if any of the keywords match the word in the verse
// Iterate through each keyword and check if its in the specific verse (has to be this way around)
$found_keyword=false;
foreach ($searchkeywords as $keyword)
{
// Is the keyword in the
unset($tmpArray);
$tmpArray[] = $verse_by_word;
if($debug==1){ echo "----------------------------------------------------------------------<br/>";}
if($debug==1){ echo "Verse that is matched for criteria : ".$verse_by_word."<br/>";}
if($debug==1){ echo "Keyword to be used in search ".$keyword."<br/>";}
$regex_string = "/".$keyword."/i";
if($debug==1){ echo "Temp array size [".sizeof($tmpArray)."]<br/>";}
$regex_string = "/".$keyword."/i";
if($debug==1){ echo "Regular Expression used in search ".$regex_string."<br/>";}
$search_result_by_keyword = preg_grep($regex_string, $tmpArray);
if($debug==1){ echo "Regex results found ".sizeof($search_result_by_keyword)."<br/>";}
if(sizeof($search_result_by_keyword) > 0)
{
$found_keyword = true;
break;
}
if($debug==1){ echo "----------------------------------------------------------------------<br/>";}
}
if($found_keyword)
{
if($debug==1){ echo "KEYWORD MATCHES CRITERIA<br/>";}
$outputverse[] = " <b>".preg_replace('/\s\./', '.', $verse_by_word)."</b> ";
}
else
{
$outputverse[] = " ".preg_replace('/\s\./', '.', $verse_by_word)." ";
}
}
echo sizeof($outputverse);
print_r($outputverse);
if(is_array($outputverse))
{
echo implode(' ' , $outputverse);
}
我使用的数组是单个数组,但作为多维数组返回。如果我打印出里面的数组,返回一个空的结果集。
print_r($outputverse[0]);
if(is_array($outputverse))
{
echo implode(' ' , $outputverse[0]);
}
我在数组上使用了
print_r()
语句,一切似乎都是正确的:
Array (
\[0\] =\> Array ( )
\[1\] =\> And
\[2\] =\> this
\[3\] =\> is
\[4\] =\> life
\[5\] =\> eternal,
\[6\] =\> that
\[7\] =\> they
\[8\] =\> might
\[9\] =\> know
\[10\] =\> thee
\[11\] =\> the
\[12\] =\> only
\[13\] =\> true
\[14\] =\> God,
\[15\] =\> and
\[16\] =\> **Jesus**
\[17\] =\> **Christ,**
\[18\] =\> whom
\[19\] =\> thou
\[20\] =\> hast
\[21\] =\> sent.
)
我还测试数组以检查它是否是一个数组(数组是一个数组):
if(is_array($outputverse))
{
echo implode(' ' , $outputverse[0]);
}
我很难过,内爆函数应该简单地内爆数组并打印出字符串值。欢迎就此问题提出任何意见。