Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中搜索数组中的子字符串_Php_Arrays_String_Substring - Fatal编程技术网

在php中搜索数组中的子字符串

在php中搜索数组中的子字符串,php,arrays,string,substring,Php,Arrays,String,Substring,嗨,我想知道是否有一个很好的算法来搜索另一个数组中的一个子字符串 我有点像: 排列( 嗯,也许像: foreach($outsideArray as $outterKey=>$outter) { foreach($outter as $innerKey=>$inner){ if(substr_count ($inner , $needle)) { ech

嗨,我想知道是否有一个很好的算法来搜索另一个数组中的一个子字符串

我有点像:

排列(

嗯,也许像:

      foreach($outsideArray as $outterKey=>$outter) {
                foreach($outter as $innerKey=>$inner){
                   if(substr_count ($inner , $needle)) {
                         echo $outterKey . " and " . $innerKey;
                    }
                }
        }
编辑:可伸缩,我在你的评论中注意到你希望它可伸缩。递归怎么样

function arraySearch($array) {
    foreach($array as $key=>$item){
        if(is_array($item)
            return arraySearch($item);
        elseif(substr_count ($item , $needle)
            return $key;
    }
}

在这里试试这段代码,你就得到了字符串的完整位置。 这里$find是您的子字符串。$data是您的数组

$find = "<img src='4' width='21' height='21' alt=' class='i-twitter-xs' />";
 foreach ($data as $out_key => $out_value) 
 {
   if(is_array($out_value))
   {
     if(in_array($find, $out_value))
     {
        $out_pos = array_search($out_value, $data);
        $inn_pos =array_search($find, $out_value);
     }
   }
 }
echo $data[$out_pos][$inn_pos];
$find=”“;
foreach($out\u key=>$out\u值)
{
if(is_数组($out_值))
{
if(in_数组($find,$out_值))
{
$out\u pos=数组搜索($out\u值,$data);
$inn\u pos=array\u search($find,$out\u value);
}
}
}
echo$data[$out\u pos][$inn\u pos];

不……如果你知道你的解决方案总是有两层深,那听起来是合理的。问题是目前它只有两层深,但我想考虑一下是否有可能扩展它:(该数组看起来像是从解析HTML的正则表达式中获得的。在这种情况下,请删除该数组并使用HTML解析器。您好,Gordon,它确实来自HTML中的正则表达式,但它不是我的,我必须使用该数组=D我收到信息并将其用作参数:)有了这个解决方案,即使我在0,0上找到了目标,我也不需要完成两个foreach吗?两个相同,我只是觉得foreach更优雅。如果找到了,就会中断循环。此外,我在上面的评论中看到,您希望它可以针对数组级别进行扩展。我发布了一个针对未知子数组级别的递归解决方案。您应该使用
strpos()
stripos()
忽略大小写)而不是
substr\u count()
,因为您只需要知道它是否包含,而不需要知道包含了多少次。谢谢,我只添加了一个中断和一些其他修改,查看更新并了解您的想法(我需要它不要忽略大小写,这就是我使用strpos()的原因)
function arraySearch($array) {
    foreach($array as $key=>$item){
        if(is_array($item)
            return arraySearch($item);
        elseif(substr_count ($item , $needle)
            return $key;
    }
}
$find = "<img src='4' width='21' height='21' alt=' class='i-twitter-xs' />";
 foreach ($data as $out_key => $out_value) 
 {
   if(is_array($out_value))
   {
     if(in_array($find, $out_value))
     {
        $out_pos = array_search($out_value, $data);
        $inn_pos =array_search($find, $out_value);
     }
   }
 }
echo $data[$out_pos][$inn_pos];