【问题标题】:Matching strings in arrays php匹配数组php中的字符串
【发布时间】: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


    【解决方案1】:

    一个名为 array_search 的函数,在数组中查找匹配项并返回键号。

    $key = array_search($a_subs_name[$c], $team_squad);
    

    这里是文档array_search

    【讨论】:

      【解决方案2】:

      team_squada_subs_name 应该是变量 $team_squad$a_subs_name

      你可以使用in_array()函数

      参考:http://in3.php.net/in_array

      【讨论】:

      • 不,我试图在数据库的每个单元格中插入是或否。不只搜索一个元素
      【解决方案3】:

      你为什么不试试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);
      

      【讨论】:

      • 我认为这是个好主意,但它似乎适用于我的数据的第一行和第二行
      • 行?没看懂可以解释一下吗?
      • 我正在从一个包含球员姓名的网站上抓取数据,并且只想存储 team_squad 中的球员。我使用了array_intersect,但到目前为止只得到了我。我重新发布代码更详细,只是重新发布代码
      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2013-09-16
      相关资源
      最近更新 更多