php数组中的字符串匹配
我有两个数组,检查它们之间是否匹配,然后根据匹配情况将yes插入数据库。其中一个数组一次接受3个元素,以与另一个静态元素进行比较 代码中的问题是它正确地比较了大多数,但我仔细查看了数据库。有几个比较是不正确的。有没有更好的方法来构造我的代码,以便不正确地比较任何内容 我发现当$team_团队中两个相邻的元素都被认为是“是”时,就会发生错过比赛的情况 这个字符串也非常正确 下面是代码的简单版本php数组中的字符串匹配,php,arrays,string,loops,multidimensional-array,Php,Arrays,String,Loops,Multidimensional Array,我有两个数组,检查它们之间是否匹配,然后根据匹配情况将yes插入数据库。其中一个数组一次接受3个元素,以与另一个静态元素进行比较 代码中的问题是它正确地比较了大多数,但我仔细查看了数据库。有几个比较是不正确的。有没有更好的方法来构造我的代码,以便不正确地比较任何内容 我发现当$team_团队中两个相邻的元素都被认为是“是”时,就会发生错过比赛的情况 这个字符串也非常正确 下面是代码的简单版本 <php //Take 3 subs at a time 3 home and 3 away p
<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);
}
?>
团队团队
和a_subs_name
应该是可变的$team_band
和$a_subs_name
您可以在数组()中使用
函数
参考:为什么不试试
这将在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);
一个名为array_search的函数,在数组中查找匹配项并返回密钥号
$key = array_search($a_subs_name[$c], $team_squad);
这是文档。否,我试图在数据库的每个单元格中插入是或否。不是只搜索一个元素我想这是个好主意,但它似乎适用于我数据行的第一行和第二行?你能解释一下吗?我正在从一个球员名字的网站上搜集数据,只想储存球队中的球员。我使用了数组_intersect,但只得到了目前为止的结果。我重新发布了更详细的代码,只是重新发布了代码