我已經有很多的麻煩此代碼工作,我想知道,如果你們可以看看它,也許看到我在做什麼錯後從MySQL檢索的陣列。更換過濾字符串
這裏是我想要做的事:
我有一個包含兩個變量$ buildname和$作家的URL。
這兩個變量插入查詢爲MySQL檢索唯一行,在那裏它再取出的該行信息的數組。例如,每個排將包含信息,例如:
ID,作者,Buildname,武器,MOD1,Mod2中..模8,極性1,極性2 ..極性8,隱藏。
然後我要檢查每一個值,看看它是否等於一個字符串,如
「在這個插槽無模」,並與
「」
更換這樣我可以在以後使用array_filter來擺脫數組的那一部分。
我的問題(S)我遇到有以下
當我取的陣列,且執行foreach循環,其回聲陣列的每個元件中,一式兩份。例如,如果我執行
foreach($ info_array as $ string) { echo $ string; }
我得到的結果
66SteelyDanSteelyDanAcrid BabiesAcrid BabiesAcridAcridNo國防部在在在在在這個slotNo MOD這個slotNo MOD這個slotNo MOD這個slotNo MOD這個slotNo國防部在此插槽
凡ID是6,作者是SteelyDan,構建名是Acrid Babies,第一個mod在這個插槽中是No mod ...等等。
爲什麼它是這樣複製的?
繼續...
我的代碼來替換這些值「N」(這將表現爲極性值)和「在這個插槽無模」(這似乎是一個模值)以下:
foreach($info_array as &$string)
{
if($string == "n")
{
str_replace("n","","n");
}
if($string == "No mod in this slot")
{
str_replace("No mod in this slot","","No mod in this slot");
}
}
我會過濾數組以擺脫任何空值。
但是,這段代碼運行不正常。
如果我呼應陣列I運行循環後,所有的值都是相同的。
到底什麼我做錯了這裏?這是我的完整代碼:
foreach($info_array as $key => $string)
{
if($string == "n" || $string == "No mod in this slot")
{
unset($info_array[$key]);
}
}
$page_id = $info_array['id'];
$page_author = $info_array['author'];
$page_buildname = $info_array['buildname'];
$page_weapon = $info_array['weapon'];
$page_mod1 = $info_array['mod1'];
$page_mod2 = $info_array['mod2'];
$page_mod3 = $info_array['mod3'];
$page_mod4 = $info_array['mod4'];
$page_mod5 = $info_array['mod5'];
$page_mod6 = $info_array['mod6'];
$page_mod7 = $info_array['mod7'];
$page_mod8 = $info_array['mod8'];
$page_polarity1 = $info_array['polarity1'];
$page_polarity2 = $info_array['polarity2'];
$page_polarity3 = $info_array['polarity3'];
$page_polarity4 = $info_array['polarity4'];
$page_polarity5 = $info_array['polarity5'];
$page_polarity6 = $info_array['polarity6'];
$page_polarity7 = $info_array['polarity7'];
$page_polarity8 = $info_array['polarity8'];
$page_category = $info_array['category'];
$page_description = $info_array['description'];
$page_date = $info_array['date'];
$page_hidden = $info_array['hidden'];
//Check if the accessing user is the same as the page creator. If not, check if page is hidden. If page is hidden, redirect to index.php.
if($_SESSION['username'] != $page_author)
{
if($page_hidden == y)
{
header("Location: index.php");
}
}
//Retrieve Page Main Image
$page_main_image = convertImageMainPageWeapon($page_weapon);
//Set up mod and polarity associative arrays
$mod_array = array(
"image_mod1" => "$page_mod1",
"image_mod2" => "$page_mod2",
"image_mod3" => "$page_mod3",
"image_mod4" => "$page_mod4",
"image_mod5" => "$page_mod5",
"image_mod6" => "$page_mod6",
"image_mod7" => "$page_mod7",
"image_mod8" => "$page_mod8"
);
$polarity_array = array(
"image_polarity1" => "$page_polarity1",
"image_polarity2" => "$page_polarity2",
"image_polarity3" => "$page_polarity3",
"image_polarity4" => "$page_polarity4",
"image_polarity5" => "$page_polarity5",
"image_polarity6" => "$page_polarity6",
"image_polarity7" => "$page_polarity7",
"image_polarity8" => "$page_polarity8"
);
foreach($mod_array as &$string)
{
if($string != "")
{
$string = convertImageMod($string);
}
}
foreach($polarity_array as &$string)
{
if($string != "")
{
$string = convertImagePolarity($string);
}
}
編輯:代碼固定。變量現在'未設置'但我收到「未定義的索引錯誤」
謝謝!
* PSA:*'mysql_ *'函數[在PHP 5.5中不推薦](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated)。不建議您編寫新的代碼,因爲這會阻止您將來升級。相反,請使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –
嘗試實際使用什麼str_replace返回'$ something = str_replace('n','',$ string);' – Anigel