我有两个数组检查它们之间是否匹配,然后根据数据库是否匹配将yes插入数据库。其中一个数组一次取3个元素,与另一个静态元素进行比较。
代码中的问题是它正确地比较了多数,但我仔细研究了数据库。一些比较是不正确的。有没有更好的方法来构建我的代码,所以没有错误的比较?
我发现在$ team_squad中彼此相邻的两个元素都假设为“是”时会发生未命中匹配。
字符串也是正确的。
以下是代码的简单版本
<php
//Take 3 subs at a time 3 home and 3 away players
for($subs = 3;$subs<=$count_subs;$subs+=3){
//Set the answers for each player to 'no' (the for loop sets all players to no).
for($data=0; $data<$squad_length; $data++){
$input[$data] = 'no';
}
//Select 3 subs home and away for each team, starting from 0 and restrict to 3 and move up by 3's.
$h_subs_name = array_slice($home_subs_name,$subs-3, $subs);
$a_subs_name = array_slice($away_subs_name,$subs-3, $subs);
//Identify the matches from the players in the team squad with the subs
$subshome = array_intersect($team_squad, $h_subs_name);
$subsaway = array_intersect($team_squad, $a_subs_name);
//if array is not empty run code
if(!empty($subshome)){
//For each match in the change the 'no' to a 'yes' from the forloop.
foreach ($subshome as $key => $value) {
# code...
//Select the index to change to a yes
$input[$key] = $y;
}
}
//if array is not empty run code
if(!empty($subsaway)){
//For each match in the change the 'no' to a 'yes' from the forloop.
foreach ($subsaway as $k => $eachplayer) {
# code...
//Select the index to change to a yes
$input[$k] = $y;
}
}
foreach ($input as $s) {
$inserttablesub[] = '"'. $s . '"';
}
$querysub = 'INSERT INTO '.$subtable.' '.'('. implode(',', $players_name_insert).') '.'VALUES'.'(' . implode(',', $inserttablesub) . ')';
mysql_query($querysub)
or die(mysql_error());
unset($subshome);
unset($subsaway);
unset($input);
unset($inserttablesub);
}
?>
team_squad用作数据库中的列名
答案 0 :(得分:2)
一个名为array_search的函数,在数组中查找匹配项并返回键编号。
$key = array_search($a_subs_name[$c], $team_squad);
以下是文档array_search。
答案 1 :(得分:0)
team_squad
和a_subs_name
应该是变量$team_squad
和$a_subs_name
您可以使用in_array()
功能
答案 2 :(得分:0)
为什么不尝试array_interset()
这将找到2个数组之间的公共元素,您可以使用该数组进行插入查询'
$team_squad = array("Wojciech Szczesny", "Per Mertesacker","Olivier Giroud","Laurent Koscielny","Bacary Sagna","Mesut Özil","Aaron Ramsey","Kieran Gibbs","Santi Cazorla","Jack Wilshere","Bacary Sagna","Nacho Monreal","Thomas Vermaelen");
$a_subs_name = array("Carl Jenkinson","Santi Cazorla","Lukas Podolski","Darren Bent","Alexander Kacaniklic","Giorgos Karagounis","Mathieu Flamini","Nacho Monreal","Bacary Sagna");
$result = array_intersect($team_squad, $a_subs_name);