Php 替换字符串后过滤从MySQL检索的数组
我一直很难让这个代码正常工作,我想知道你们是否可以看看它,也许看看我做错了什么 以下是我想做的: 我有一个URL,它包含两个变量$buildname和$author 这两个变量被插入到Mysql的查询中,以检索一个唯一的行,然后从中获取该行的一个信息数组。例如,每行将包含以下信息: ID,作者,建筑名,武器,Mod1,Mod2。。模块8,极性1,极性2。。极性8,隐藏 然后我想检查每个值,看看它是否等于一个字符串,比如 “此插槽中没有mod” 并将其替换为 “” 因此,我可以在以后使用array_filter除去数组的该部分 我遇到的问题如下Php 替换字符串后过滤从MySQL检索的数组,php,mysql,Php,Mysql,我一直很难让这个代码正常工作,我想知道你们是否可以看看它,也许看看我做错了什么 以下是我想做的: 我有一个URL,它包含两个变量$buildname和$author 这两个变量被插入到Mysql的查询中,以检索一个唯一的行,然后从中获取该行的一个信息数组。例如,每行将包含以下信息: ID,作者,建筑名,武器,Mod1,Mod2。。模块8,极性1,极性2。。极性8,隐藏 然后我想检查每个值,看看它是否等于一个字符串,比如 “此插槽中没有mod” 并将其替换为 “” 因此,我可以在以后使用array
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);
}
}
编辑:代码已修复。变量现在“未设置”,但我收到“未定义的索引错误”
谢谢 与其将空值放入数组,然后在以后使用
array\u filter
,不如删除数组元素:
foreach($info_array as $key => $string)
{
if($string == "n" || $string == "No mod in this slot")
{
unset($info_array[$key]);
}
}
试试这个
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的引号,添加了变量赋值,最初略读了太多内容,没有注意到还没有完成。PSA:mysql*函数是。不建议编写新代码,因为这样会阻止您将来升级。相反,请使用或和。尝试实际使用str_replace返回的内容
$something=str_replace('n','',$string)代码>$string=str_replace(“n”、“”、$string)代码>@TobiasKun-谢谢。我赶时间,因为午餐已经准备好了。没问题。享受你的美食^^这是可行的,但当它运行时,它会在C:\web\view\U build.php的第72行返回通知:通知:未定义索引:polarity8。。。我的数组过滤器是否应该处理这些空值并删除包含空值的数组行?是的,我正在使用新代码。它工作得很好,我喜欢这个想法,但是,我得到了这些错误:注意:第44行的C:\web\view\u build.php中的未定义索引:mod1注意:第45行的C:\web\view\u build.php中的未定义索引:mod2注意:第46行的C:\web\view\u build.php中的未定义索引:mod3等等。稍后在我的代码中,我引用$info\u数组,因为我想将每个字符串转换为图像标记。但是没有设置任何内容,它会看到“undefined index”看到我的代码——我将要更新它。除了原始脚本之外,它不会做任何其他事情,因为str_replace会返回更改后的字符串。