简体   繁体   English

在PHP数组中搜索子字符串

[英]Searching for a substring inside an array in php

Hi I would like to know if there is a good algorithm to the search of a substring inside an array that is inside another array, 嗨,我想知道是否有一种好的算法来搜索另一个数组内部的数组内的子字符串,

I have something like: 我有类似的东西:

Array( 数组(

        [0] => Array(
                    [0] => img src="1" /> 
                    [1] => img src="2" alt="" class="logo i-dd-logo" /> 
                    [2] => img src="3" alt="" /> 
                    [3] => img src="4" width="21" height="21" alt="" class="i-twitter-xs" /> 
                    [4] => img src="myTarget" width="21" height="21" alt="" class="i-rss" /> 
                    [5] => <img class="offerimage" id="product-image" src="6" title="" alt=""/> 
                    [6] => <img class="offerimage" id="product-image" src="7" title="" alt=""/> 
                    [7] => <img class="offerimage" id="product-image" src="8" title="" alt=""/> 
                    [8] => <img src="9" width="16" height="16" /> 
    )

[1] => Array(
                    [0] =>  src="1" 
                    [1] =>  src="a" alt="" class="logo i-dd-logo" 
                    [2] =>  src="b" alt="" 
    )

)

What I want to do is to know the position of target, for example [0][4] but it's not always the same 我想做的就是知道目标的位置,例如[0] [4],但并不总是相同

What I'm doing now is a while inside another while and checking whith strpos for the substring, but maybe there is a better way to do this, any suggestions? 我现在正在做的是在另一个内部进行一会儿,并检查是否有子字符串的strpos,但是也许有更好的方法来执行此操作,有什么建议吗?

Thanks for everything 谢谢你所做的一切


Updated code: 更新的代码:

$i=-1; $ i = -1;

foreach($img as$outterKey=>$outter) { foreach($ img as $ outterKey => $ outter){

  foreach($outter as $innerKey=>$inner){ $pos = strpos($img[$outterKey][$innerKey],"myTarget"); if (!$pos === false) { $i=$outterKey;$j=$innerKey; break 2; } } } 

try this code here you got the complete posistion of your string. 在这里尝试此代码,您将获得字符串的完整信息。 here $find is your substring. $ find是您的子字符串。 $data is your array. $ 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];

Umm, maybe like: 嗯,也许像:

      foreach($outsideArray as $outterKey=>$outter) {
                foreach($outter as $innerKey=>$inner){
                   if(substr_count ($inner , $needle)) {
                         echo $outterKey . " and " . $innerKey;
                    }
                }
        }

EDIT: Scalable, I noticed in your comments you want it scalable. 编辑:可扩展,我在您的评论中注意到您希望它可扩展。 How about recursive? 递归如何?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM