Note that there are some explanatory texts on larger screens.

plurals
  1. POforeach count does not get updated after array_splice
    text
    copied!<p>I have two large arrays:</p> <pre><code>$a = array('a1','a2','a3','a4'); // and so on $b = array('b1'=&gt;'a1', 'b2'=&gt;'a3', 'b3'=&gt;'a1'); // </code></pre> <p>I would like to get the following result:</p> <pre><code>$a = array('a1'=&gt;array('b1', 'b3'), 'a2'=&gt;array(), 'a3'=&gt;array('b2') ); </code></pre> <p>I could just do:</p> <pre><code>foreach($a as $aa){ foreach($b as $bb){ // check with if then add to a } } </code></pre> <p>but it would get tremendously large with bigger numbers. </p> <p>So it occurred to me that if i remove each 'b' element after being added to <code>$a</code> the next loops will be smaller, and i would cut on resources. </p> <p>However when splicing the looped array, the index does not seem to get updated, and the next loop does not take into consideration that fact that it was cut down by 1. </p> <p>How can I make this work, and also, is there a better way of fitting items of an array into the appropriate indexes of another array?</p> <p>EDIT:</p> <p>how would this be done if the structure of both <code>$a</code> and <code>$b</code> were:</p> <pre><code>$a[0]['Word']['id']=1; $a[0]['Word']['sentence_id']=2; $a[0]['Word']['word_string']='someWord'; $b[0]['Word']['id']=3; $b[0]['Word']['sentence_id']=4; $b[0]['Word']['word_string']='someWord'; // And i would like to list `b` like so: $a[0]['list_of_bs']=array(b[0], b[1]); //So that i can get: echo $a[0]['list_of_bs']['Word']['id']; // and to get result 3 </code></pre> <p>and i would like it to be <code>$a[0][Word][list_of_b]=array(b1,b2,b3)</code> and each of the <code>b</code>'s has it's own data in associative array.</p>
 

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