用逗号分隔的数组php搜索字符串

时间:2014-02-06 06:15:06

标签: php

我需要返回包含范围内搜索字符串的数组。 基本上我想用pakistan1搜索字符串,并且不要超过它应该忽略带键0的数组 第二个字符串bangladesh4它应该忽略4以下的任何其他键 最终返回数组应为

array(
[2]=>
[3]=>
[4]=>
)

需要搜索两个字符串。我该怎么办?

  

第一:巴基斯坦2

     

第二名:孟加拉国4

$input_arr= array(
    0=>array(india0,srilanka1,pakistan0,banglades0),
    1=>array(india1,srilanka1,pakistan1,bangladesh1),
    2=>array(india2,srilanka2,pakistan2,bangladesh2),
    3=>array(india3,srilanka3,pakistan3,bangladesh3),
    4=>array(india 4,srilanka4,pakistan4,bangladesh4),
    5=>array(india 5,srilanka5,pakistan5,bangladesh5),
);

我想将结果数组返回为:

$result_arr= array(
    2=>array(india2,srilanka2,pakistan2,bangladesh2),
    3=>array(india3,srilanka3,pakistan3,bangladesh3),
    4=>array(india 4,srilanka4,pakistan4,bangladesh4)
)

EDITED

$first_str = "pakistan2";
$second_str = "bangladesh4";


$arr_output = array();
foreach($input_arr as $key=>$temp_arr)
{

    if(in_array($first_str, $temp_arr) || in_array($second_str, $temp_arr) )
    {
        $arr_output[$key]=$temp_arr;
    }
}

无法得到 阵列3

2 个答案:

答案 0 :(得分:1)

试试这个

  $result_arr= array();
    for($i=0;$i<sizeof($input_arr);$i++)
    {
     if(in_array("searchstring",$input_arr[$i]))
      if(in_array("searchstring2",$input_arr[$i]))
        $result_arr = $input_arr[$i];
    }

答案 1 :(得分:1)

试试这个

$input_arr= array(
    0=>array("india0","srilanka1","pakistan0","banglades0"),
    1=>array("india1","srilanka1","pakistan1","bangladesh1"),
    2=>array("india2","srilanka2","pakistan2","bangladesh2"),
    3=>array("india3","srilanka3","pakistan3","bangladesh3"),
    4=>array("india 4","srilanka4","pakistan4","bangladesh4"),
    5=>array("india 5","srilanka5","pakistan5","bangladesh5"),
);

$first_str = "pakistan2";
$second_str = "bangladesh4";


$arr_output = array();
foreach($input_arr as $key=>$temp_arr)
{

    if(in_array($first_str, $temp_arr) || in_array($second_str, $temp_arr) )
    {
        $arr_output[$key]=$temp_arr;
    }
}

print_r($arr_output);