Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching for a substring inside an array in php
    text
    copied!<p>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,</p> <p>I have something like:</p> <p>Array(</p> <pre><code> [0] =&gt; Array( [0] =&gt; img src="1" /&gt; [1] =&gt; img src="2" alt="" class="logo i-dd-logo" /&gt; [2] =&gt; img src="3" alt="" /&gt; [3] =&gt; img src="4" width="21" height="21" alt="" class="i-twitter-xs" /&gt; [4] =&gt; img src="myTarget" width="21" height="21" alt="" class="i-rss" /&gt; [5] =&gt; &lt;img class="offerimage" id="product-image" src="6" title="" alt=""/&gt; [6] =&gt; &lt;img class="offerimage" id="product-image" src="7" title="" alt=""/&gt; [7] =&gt; &lt;img class="offerimage" id="product-image" src="8" title="" alt=""/&gt; [8] =&gt; &lt;img src="9" width="16" height="16" /&gt; ) [1] =&gt; Array( [0] =&gt; src="1" [1] =&gt; src="a" alt="" class="logo i-dd-logo" [2] =&gt; src="b" alt="" ) </code></pre> <p>)</p> <p>What I want to do is to know the position of target, for example [0][4] but it's not always the same</p> <p>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?</p> <p>Thanks for everything</p> <hr> <p>Updated code:</p> <blockquote> <p>$i=-1; </p> <p>foreach($img as$outterKey=>$outter) {</p> <pre><code> foreach($outter as $innerKey=&gt;$inner){ $pos = strpos($img[$outterKey][$innerKey],"myTarget"); if (!$pos === false) { $i=$outterKey;$j=$innerKey; break 2; } } } </code></pre> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload