我有一个数组:
$mainArr = ["SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS"];
$strArr = ["SRI"];
我想用给定的字符串数组元素搜索主数组,这样如果字符串匹配,它应该得到相应的键值对。
预期的o / p将是:
Array[
0->SRI
3->SRI#AIS
4->SRI#GOW
5->SRI#AIS#GOW
]
有什么想法吗?
谢谢, SRINIVAS
答案 0 :(得分:1)
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$strArr = array("SRI");
foreach ($mainArr as $key => $value)
{
foreach ($strArr as $str)
{
if (strpos($value,$str) !== false) $rez[$key] = $value;
}
}
var_dump($rez);
输出:
array(4) {
[0]=> string(3) "SRI"
[3]=> string(7) "SRI#AIS"
[4]=> string(7) "SRI#GOW"
[5]=> string(11) "SRI#GOW#AIS"
}
答案 1 :(得分:0)
使用array_filter
:
function filter($element)
{
return strpos($element, 'SRI') !== false;
}
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$filteredArr = array_filter($mainArr, 'filter');
答案 2 :(得分:0)
我会帮助你,
$test=array();
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$strArr = array("SRI");
foreach ($mainArr as $key => $value)
{
$temp = explode('#',$value);
//$temp = $temp[0];
if(in_array($temp[0],$strArr))
$test[$key]=$value;
}
echo "<pre><span style='color:black; font-size:19;'>";print_r($test);echo "</span></pre>";