【发布时间】: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