2014-01-18 4 views
1

두 개의 배열이 있는데 두 배열 사이에 일치가 있는지 확인한 다음 둘 다 일치하는 경우 데이터베이스에 yes를 삽입합니다. 배열 중 하나는 정적 인 다른 요소와 비교하기 위해 한 번에 3 개의 요소를 사용합니다.배열의 일치하는 문자열 PHP

코드의 문제점은 대다수를 정확하게 비교하고 있지만 데이터베이스를 자세히 살펴 본 점입니다. 몇 가지 비교가 잘못되었습니다. 아무 것도 잘못 비교되지 않도록 내 코드를 구성하는 더 좋은 방법이 있습니까?

$ 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의 간단한 버전입니다 $team_squad$a_subs_name

사용할 수

답변

2

함수라는 array_search, 외모에 대한 배열을 사용할 수 있습니다 array_interset()

를 시도하지 말아 배열에서 일치하는 항목을 찾고 키 번호를 반환합니다.

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

다음은 문서 array_search입니다.

0

team_squada_subs_name 변수해야 데이터베이스에 열 이름으로 사용됩니다 in_array() 기능

참조 : http://in3.php.net/in_array

+0

아니요, 데이터베이스의 각 셀에 예 또는 아니요를 삽입하려고합니다. 단 하나의 요소를 검색하지 않습니다 – user3210416

0

왜이 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); 
+0

이게 효과가있는 것 같아요. 시도해 보겠습니다. – user3210416

+0

이것이 좋은 생각이 될 것이라고 생각했지만 데이터의 첫 번째와 두 번째 행에서 작동하는 것 같습니다. – user3210416

+0

행? 네가 조금 설명해 주겠니? –