【发布时间】:2014-02-08 02:27:02
【问题描述】:
我有两个数组,它们检查它们之间是否匹配,然后根据是否匹配将 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 用作数据库中的列名
【问题讨论】:
标签: php arrays string loops multidimensional-array