0

我在让这段代码正常工作时遇到了很多麻烦,我想知道你们是否可以看看它,也许看看我做错了什么。

这是我正在尝试做的事情:

我有一个包含两个变量 $buildname 和 $author 的 URL。

这两个变量被插入到 Mysql 的查询中以检索唯一的行,然后它从中获取该行的信息数组。例如,每一行将包含以下信息:

ID,作者,Buildname,Weapon,Mod1,Mod2 .. Mod 8,Polarity 1,Polarity 2.. Polarity 8,隐藏。

然后我想检查每个值,看看它是否等于一个字符串,例如

“此插槽中没有模组”

并将其替换为

“”

这样我以后可以使用 array_filter 摆脱数组的那部分。

我遇到的问题如下

  1. 当我获取数组并执行一个回显数组每个元素的 foreach 循环时,它是重复的。例如,如果我执行

    foreach($info_array as $string) { echo $string; }

我得到结果

66SteelyDanSteelyDanAcrid BabiesAcrid BabiesAcridAcrid此插槽中没有模组此插槽中没有模组此插槽中没有模组此插槽中没有模组此插槽中没有模组此插槽中没有模组

其中 ID 为 6,作者为 SteelyDan,buildname 为 Acrid Babies,第一个 mod 为 No mod in this slot……以此类推。

为什么会这样重复?

继续...

我替换这些值“n”(显示为极性值)和“No mod in this slot”(显示为 mod 值)的代码如下:

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");
    } 
}

然后我会过滤数组以去除任何空值。

但是,此代码无法正常执行。

如果我在运行循环后回显数组,则所有值都是相同的。

我到底在做什么错在这里?这是我的完整代码:

    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);
    }
}

编辑:代码已修复。变量现在“未设置”,但我收到“未定义的索引错误”

谢谢!

4

2 回答 2

1

与其将空值放入数组中,然后再使用array_filter,不如直接删除数组元素:

foreach($info_array as $key => $string)
{
    if($string == "n" || $string == "No mod in this slot")
    {
        unset($info_array[$key]);
    }
}
于 2013-07-30T15:29:40.353 回答
0

试试这个

foreach($info_array as &$string)
{
    if($string == "n")
    {
        $string = str_replace("n", "", $string);
    }
    if($string == "No mod in this slot")
    {
        $string = str_replace("No mod in this slot", "", $string);
    } 
}

[编辑] 删除了 str_replace 中 $string 周围的引号,添加了变量赋值,最初浏览了太多并且没有注意到这还没有完成。

于 2013-07-30T15:32:00.953 回答