Access variable from inside a nested foreach statement PHP
<p>I am getting the key from a given value in a multidimensional array. It works fine except that I cannot seem to access the variable from OUTSIDE the nested foreach loop that I'm using to get the key.</p>

<p>so my foreach loop is: ($name_books is the multi-d array which contains 3 smaller arrays)</p>

<pre><code>foreach($name_books as $test) {
    foreach ($test as $key =&gt; $value) {
    $book_code = array_search($row['name'],$test);
    echo $book_code; //just to see if it works, which it does
    break;
        } 
    }
//But then if I go outside of the loop..

echo $book_code." is the book code"; // &lt;--DOES NOT WORK
</code></pre>

<p>So I know that I'm dealing with variable scope issues here and I've tried declaring globals inside the foreach loop but nothing works.</p>

<p>I'm sure there is something absurdly simple I'm missing!</p>

<p>EDIT:</p>

<p>urg..I took a step back and realized something else,
all this is happening inside a while loop (getting stuff from a db)</p>

<p>so the code is more like:</p>

<pre><code>while($row=mysql_fetch_assoc($result)) {

   ...original foreach loop from above

}
</code></pre>

<p>apologies for not including this, I was focusing on this tiny piece and forgot to back up and see where it fit. </p>
