Php 显示组合列表时出现问题
我面临一个问题。。。。请帮帮我 我有两个字符串:Php 显示组合列表时出现问题,php,arrays,list,foreach,Php,Arrays,List,Foreach,我面临一个问题。。。。请帮帮我 我有两个字符串: $teamslist = BAL@DET,WAS@SF,LA@STL,LAA@KC; $string = BAL,WAS,LA,LAA,DET,SF,STL,KC; 我想检查的是$string元素是否在$teamslist中找到,而不是它在$teamslist中显示全名 e、 g: 我在$string中有“BAL”,我想检查是否在$teamlist中找到它。。。如果找到了,结果应该是 $result will be = BAL@DET 我正在
$teamslist = BAL@DET,WAS@SF,LA@STL,LAA@KC;
$string = BAL,WAS,LA,LAA,DET,SF,STL,KC;
我想检查的是$string元素是否在$teamslist中找到,而不是它在$teamslist中显示全名
e、 g:
我在$string中有“BAL”,我想检查是否在$teamlist中找到它。。。如果找到了,结果应该是
$result will be = BAL@DET
我正在使用的代码适用于一个条件,但不适用于第二个条件。。。如果在@符号之前找到BAL,则其显示结果正确。。但是,如果在@符号后找到它,它有时会显示一个字母或什么也不显示。。请帮我解决我的第二个问题。。。若单词在@符号之前或之后,它将显示两个符号的正确结果
希望这能理解我的问题。。我正在尝试的代码是:
foreach($string as $tag)
{
$teamslisto = substr($teamslist, strpos($teamslist, $tag)+strlen($tag));
$teamslisto2 = substr($teamslist, strpos($teamslist, $tag) - strlen($tag) -1);
$final=explode(",",$teamslisto);
$final2=explode(",",$teamslisto2);
if($final['0']=="")
{
$opkplay = $final2['0'];
}
else
{
$opkplay = $tag.$final['0'];
}
}
请帮帮我 在这种情况下也可以使用stripos()
:
修订版:stripos可能会进行虚假匹配,从而使团队爆发,而不是在阵列中
$teamslist = 'BAL@DET,WAS@SF,LA@STL,LAA@KC';
$string = 'BAL,WAS,LA,LAA,DET,SF,STL,KC';
$teamslist = explode(',', $teamslist);
$string = explode(',', $string);
$result = array();
foreach($string as $tag) {
foreach($teamslist as $teams) {
$temp = explode('@', $teams);
if(in_array($tag, $temp)) {
$result[$tag] = $teams;
}
}
}
echo '<pre>';
print_r($result);
$teamslist='1〕BAL@DET,WAS@SF,LA@STL,LAA@KC';
$string='BAL、WAS、LA、LAA、DET、SF、STL、KC';
$teamslist=分解(“,”,$teamslist);
$string=分解(“,”,$string);
$result=array();
foreach($string作为$tag){
foreach($teams列为$teams){
$temp=爆炸(“@”,$teams);
if(在数组中($tag,$temp)){
$result[$tag]=$teams;
}
}
}
回声';
打印(结果);
您可以使用preg\u match()进行尝试。
:
这将导致:
$teamslist = "BAL@DET,WAS@SF,LA@STL,LAA@KC";
$string = "BAL,WAS,LA,LAA,DET,SF,STL,KC";
$strings = explode(',', $string);
$result = array();
foreach($strings as $str)
{
$escaped = preg_quote($str); // just in case..
if (preg_match_all('/(^|,)([^@]+@' . $escaped. '|' . $escaped . '@[^@]+)($|,)/iU',
$teamslist, $matches))
$result[$str] = $matches[2];
}
var_dump($result);
结果
值是数组,以防有几个团队使用相同的部分。好的,这很好,但无论如何,它可以用我上面发布代码的相同方式完成。。。cz我已经为每个循环准备了很多内容,所以我不想再包含一个新的循环。…@DeDevelopers不,这个答案中的两个循环都很好,因为
$teamslist
和$string
都必须分解,您还需要检查每个团队列表中的每个分解的$string
,您的代码将在LAA@KC
-不要认为是这样correct@DeDevelopers你可以试试这个修改谢谢你,伙计。。我用这种方式提供了我想要的东西。。谢谢。。。
array (size=4)
0 => string 'BAL@DET' (length=7)
1 => string 'WAS@SF' (length=6)
2 => string 'LA@STL' (length=6)
3 => string 'LAA@KC' (length=6)
$teamslist = "BAL@DET,WAS@SF,LA@STL,LAA@KC";
$string = "BAL,WAS,LA,LAA,DET,SF,STL,KC";
$strings = explode(',', $string);
$result = array();
foreach($strings as $str)
{
$escaped = preg_quote($str); // just in case..
if (preg_match_all('/(^|,)([^@]+@' . $escaped. '|' . $escaped . '@[^@]+)($|,)/iU',
$teamslist, $matches))
$result[$str] = $matches[2];
}
var_dump($result);
array(8) {
["BAL"]=>
array(1) {
[0]=>
string(7) "BAL@DET"
}
["WAS"]=>
array(1) {
[0]=>
string(6) "WAS@SF"
}
["LA"]=>
array(1) {
[0]=>
string(6) "LA@STL"
}
["LAA"]=>
array(1) {
[0]=>
string(6) "LAA@KC"
}
["DET"]=>
array(1) {
[0]=>
string(7) "BAL@DET"
}
["SF"]=>
array(1) {
[0]=>
string(6) "WAS@SF"
}
["STL"]=>
array(1) {
[0]=>
string(6) "LA@STL"
}
["KC"]=>
array(1) {
[0]=>
string(6) "LAA@KC"
}
}