[英]PHP foreach loop messes up my in_array function
I've written a script that checks bulk input of names received via a textarea and omits all values that are already in the database. 我编写了一个脚本,该脚本检查通过文本区域接收的名称的批量输入,并忽略数据库中已经存在的所有值。 It works if you enter just one repeated name.
如果您只输入一个重复的名称,它将起作用。 If you enter two or more, it will filter out the first repeated name, treat the rest as unique names and insert them in the database.
如果输入两个或多个,它将过滤掉第一个重复的名称,将其余名称视为唯一名称,并将其插入数据库中。 I can't figure out why.
我不知道为什么。
Firstly, this is an array that is built in another part of the script. 首先,这是在脚本另一部分中构建的数组。 It's generated from a database query:
它是通过数据库查询生成的:
Array
(
[0] => john
[1] => peter
[2] => max
[3] => jake
)
This array is referred to as $onlyHandles. 该数组称为$ onlyHandles。 Then this is the script:
然后这是脚本:
if((isset($_POST['extract']) && !empty($_POST['extract']))){
$handles = trim($_POST['extract']);
$handles = explode("\n", $handles);
if(count($handles)>200){
echo 'error';
exit(1);
}
foreach($handles as $handle) {
$handleRep = strtolower(str_replace('@','',$handle));
$handleClean = str_replace(str_split('\\/:*?&"<>=+-#%$|'), ' ', $handleRep, $count);
if ($count > 0) {
echo 'error';
exit(1);
}
else{
if (in_array($handleClean, $onlyHandles)){
$delmessage .= "<p>".$handleClean." is already in your list.</p>";
}
else{
$sqlIns = "INSERT INTO...blah blah blah)";
$resultIns = mysql_query($sqlIns);
$resInsArr[] = array($resultIns);
}
}
}
$countresIns = count($resInsArr);
if ($countresIns > 0){
$delmessage .= "<p>User(s) added to list succesfully!</p>" ;
}
}
Now, if you enter "john" in the textarea, it will shout that the name already exists. 现在,如果您在文本区域中输入“ john”,它将大喊该名称已存在。 If you enter "john" and "max" it will omit john and add max.
如果输入“ john”和“ max”,它将省略john并添加max。
Any help would be greatly appreciated. 任何帮助将不胜感激。
PS regarding the query format, I know, I know, thanks! PS关于查询格式,我知道,我知道,谢谢!
I would like to give U some idea about how to achieve it: 我想给U一些关于如何实现它的想法:
Replace the first line: 替换第一行:
if((isset($_POST['extract']) && !empty($_POST['extract']))){ if((isset($ _ POST ['extract'])&&!empty($ _ POST ['extract']))){
through 通过
if((!empty($_POST['extract']))){
because !empty
already guives U the guaranty that it isset
由于
!empty
已经guives U中的担保,这isset
U could also use the power of Regular Expression to replace the unneeded chars in replacing: 在替换时,U还可以使用正则表达式的功能替换不需要的字符:
Line 12: $handleClean = str_replace(str_split('\\\\/:*?&"<>=+-#%$|'), ' ', $handleRep, $count);
第12行:
$handleClean = str_replace(str_split('\\\\/:*?&"<>=+-#%$|'), ' ', $handleRep, $count);
Through: 通过:
$handleClean = preg_replace("/\\[\\/:\\*?&\\"<>=\\+-#%\\$\\|\\]*/", ' ', $handleRep, $count);
line 2: $handles = trim($_POST['extract']);
第2行:
$handles = trim($_POST['extract']);
through (trim is not necessary hier) 通过(修剪不是必须的hier)
$handles = $_POST['extract'];
AND 和
line 11: $handleRep = strtolower(str_replace('@','',$handle));
第11行:
$handleRep = strtolower(str_replace('@','',$handle));
through 通过
$handleRep = trim(strtolower(str_replace('@','',$handle)));
Hey ;-), 嘿;-),
U should also add some print_r(...) to debug each step 您还应该添加一些print_r(...)来调试每个步骤
thanks @Ulrich Tevi Horus that made my code a bit cleaner but didn't solve the mysteriously disappearing users. 感谢@Ulrich Tevi Horus,这使我的代码更简洁了,但没有解决神秘消失的用户的问题。 @shulard, you should post this as an answer to get the upvote.
@shulard,您应该将其发布为答案以得到支持。 array_diff was indeed the best solution.
array_diff确实是最好的解决方案。 Here's the final code.
这是最终代码。 Needs some tidying up but it's good enough to go on my server for testing.
需要整理一下,但足以在我的服务器上进行测试。
//this is the current contents of the list:
$onlyHandles = array();
foreach ($organizedArray as $key2 => $val2) {
$onlyHandles[] = $val2['name'];
}
echo "<br>begin onlyhandles<br>";
print_r($onlyHandles);
echo "<br>end onlyhandles<br>";
//finish preparation for display
//if names submitted for the list list
if(!empty($_POST['extract'])){
$handles = trim($_POST['extract']);
$handles = explode("\n", $handles); //this is now an array
echo "<br>begin handles<br>";
print_r($handles);
echo "<br>end handles<br>";
//$countInput = count($handles);
if($countInput>200){
echo '<p style="color:red;">Please Enter fewer than 200 names!</p>';
exit(1);
}
else{
$handleFinal = array();
foreach($handles as $handle) {
//$handleRep = strtolower(str_replace('@','',$handle));
$handleRep = trim(strtolower(str_replace('@','',$handle)));
$handleClean = str_replace(str_split('\\/:*?&"<>=+ -#%$|'), 'p', $handleRep, $count);
//$handleClean = preg_replace("/\[\/:\*?&\"<>=\+-#%\$\|\s+\]*/", ' ', $handleRep, $count);
echo "handleClean: ".$handleClean."<br>";
if ($count > 0) {
echo '<p style="color:red;">Your input contained special characters.</p>';
exit(1);
}
else{
$handleFinal[] = $handleClean;
}
}//end foreach
}//finish checking count input number
echo "<br>begin handleFinal<br>";
print_r($handleFinal);
echo "<br>end handleFinal<br>";
$countFinal = count($handleFinal);
echo "<br>countfinal is ".$countFinal."<br>";
//check if this user is already in the list
$handleDiffs = array_diff($handleFinal,$onlyHandles);
echo "<br>begin handlediffs<br>";
print_r($handleDiffs);
echo "<br>end handlediffs<br>";
foreach($handleDiffs as $handleDiff) {
$sqlIns = "blah blah blah";
$resultIns = mysql_query($sqlIns);
$resInsArr[] = array($resultIns);
}
$countresIns = count($resInsArr);
if ($countresIns > 0){
$delmessage .= "<p>User(s) added to the list succesfully!</p>" ;
}
}
I post my comment answer as a real answer :) 我发表我的评论答案作为一个真正的答案:)
You must trim your $handle
variable too because it's possible to have some spaces around it... 您还必须修剪
$handle
变量,因为它周围可能有一些空格...
Then about your problem, I don't understand it. 那关于你的问题,我听不懂。 Your code seems "clean", maybe you should consider set the
strict
flag to true
see function definition here . 您的代码看起来“干净”,也许您应该考虑将
strict
标志设置为true
请参见此处的函数定义 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.