【问题标题】:Searching for a substring inside an array in php在php中搜索数组中的子字符串
【发布时间】:2011-02-11 07:32:20
【问题描述】:

您好,我想知道是否有一个好的算法可以在另一个数组内的数组内搜索子字符串,

我有类似的东西:

数组(

        [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="" 
    )

)

我想做的是知道目标的位置,例如 [0][4] 但它​​并不总是一样

我现在正在做的是一段时间内的一段时间,并检查 strpos 的子字符串,但也许有更好的方法可以做到这一点,有什么建议吗?

感谢一切


更新代码:

$i=-1;

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;
              }
            }
    }

【问题讨论】:

  • 不...如果您知道它总是 2 层深,您的解决方案听起来很合理。
  • 问题是现在它只有 2 层深,但我想考虑扩展它的可能性:(
  • 该数组看起来像是从正则表达式解析 HTML 中得到的。在这种情况下,删除数组并使用 HTML 解析器。
  • 嗨,戈登,确实它来自 html 中的正则表达式,但它不是我的,我必须使用该数组 =D 我收到信息并且必须使用它作为参数:)跨度>

标签: php arrays string substring


【解决方案1】:

嗯,也许像:

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

编辑:可扩展,我注意到在您的 cmets 中您希望它可扩展。递归呢?

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

【讨论】:

  • 使用这个解决方案,即使我在 0,0 上找到目标,我也不必完成两个 foreach 吗?
  • 两个while会一样,我只是觉得foreach更优雅。如果找到一个中断,就会跳出循环。
  • 另外,我在上面看到您希望它可扩展为您的 cmets 中的数组级别。我发布了未知子数组级别的递归解决方案。
  • 你应该使用strpos()stripos()忽略大小写)而不是substr_count(),因为你只需要知道它是否被包含而不是多少次。
  • 谢谢,我只是添加了一个中断和其他一些修改,看看更新,看看你的想法(我需要它不要忽略大小写,这就是我使用 strpos() 的原因
【解决方案2】:

在此处尝试此代码,您将获得字符串的完整位置。 这里 $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];

【讨论】:

    猜你喜欢
    • 2018-11-28
    • 2012-09-12
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2013-01-09
    • 2013-05-25
    • 2022-01-23
    • 2011-10-16
    相关资源
    最近更新 更多