我遇到了「無法重新聲明」錯誤,我無法弄清楚如何解決它。所以我在下面的php文件中有幾個函數。現在這些函數遍歷數據數組。無法重新聲明先前聲明的函數php
我想我已經猜測問題是我在foreach循環中一遍又一遍地循環函數,而且它的foreach循環就是這個問題。它似乎已經寫出了第一次記憶的功能,然後出於某種原因它不喜歡再次被喚醒。
您的幫助表示讚賞。
P.S我見過很多類似帖子,如Fatal error: Cannot redeclare,但似乎並不奏效。
<?php
// *****Code Omitted from Stack****
function postHelper($data, $field1, $field2)
{ //TODO Abstract and make sure post Helper and modify Post can be the same thing.
$result = array();
for ($j = 0; $j < count($data); ++$j) { //iterator over array
if ($field2 == "") {
$result[$j] = $data[$j][$field1];
} else {
return $result[$j] = $data[$j][$field1][$field2];
}
}
return $result;
}
//returns an array with only @ and # values
function modifyPost($data)
{
//puts symbol @ before read data
function addSymbol($data, $field1, $field2)
{
$info = postHelper($data, $field1, $field2);
foreach ($info as &$n) {
$n = '@' . $n;
}
print_r($info);
}
/*
Parse texts and returns an array with only @ or # signs used
*/
function parseText($data)
{
$newarr = array();
$text = postHelper($data, "text", "");
foreach ($text as &$s) { //separates into words
$ex = explode(" ", $s);
foreach ($ex as &$n) { //if text doesnt' begin with '@' or '#' then throw it out.
if (substr($n, 0, 1) === '@' || strpos($n, '#') !== false) {
array_push($newarr, $n . ',');
}
}
}
return $newarr;
}
}
foreach ($posts as $entry) {
if (!function_exists('modifyPost')) {
$nval = "hello";
modifyPost($entry);
$entry['mod_post'] = $nval;
}
}
?>
編輯:我已經解決了這個錯誤。原來的帖子確實有效。我搞砸了命名。我會給任何能向我解釋爲什麼這是通話必要的人的觀點。此外,如果還有其他問題,我會更新帖子。
謝謝!這正是我所期待的。任何特定的原因PHP不允許嵌套函數?這似乎是有用的。 – and0rsk 2014-08-30 12:35:50
爲什麼PHP將嵌套函數讀爲全局函數?這對我來說並不直觀。 – and0rsk 2014-08-30 12:38:10
@和0rsk:爲什麼?我不知道,但看到編輯。 – georg 2014-08-30 12:53:51