开发者

Access variable from inside a nested foreach statement PHP

开发者 https://www.devze.com 2023-01-30 13:27 出处:网络
I am getting the key from a given value in a mult开发者_开发知识库idimensional array. It works fine except that I cannot seem to access the variable from OUTSIDE the nested foreach loop that I\'m usin

I am getting the key from a given value in a mult开发者_开发知识库idimensional 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.

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

foreach($name_books as $test) {
    foreach ($test as $key => $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"; // <--DOES NOT WORK

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.

I'm sure there is something absurdly simple I'm missing!

EDIT:

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

so the code is more like:

while($row=mysql_fetch_assoc($result)) {

   ...original foreach loop from above

}

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


break;

Will only exit the internal nested foreach. If there are more rows in $name_books, it will continue looping and eventually overwriting $book_code with 'false' values from array_search;

Once you've found the value you're looking for, use:

break 2;

Regarding your edit, where you break depends on what you're doing with the value you've found for $book_code. If you don't plan on continuing, change the parameter for break. break 3; will exit the while loop too. Change the value depending on the level of nesting.


This has nothing to do with variable scope so long as what you posted is exactly what you have in your script.

I think what the problem is, is that you are only breaking out of the inner loop. In each iteration of the outer loop, $book_code will get changed, so you need to stop the outer loop as well. Try changing break; to break 2; and see if it fixes your problem. That causes it to break out of both the inner and the outer loop.

Edit: I think you can also simplify your code:

foreach ($name_books as $test) {
    $book_code = array_search($row['name'], $test);
    if ($book_code !== FALSE) {
        break;
    }
}
If I knew more about your structure, this could possibly be reduced down to a single SQL statement and 0 loops.


simshaun is right, but I would actually take a different approach.

I would check for the existence of $book_code in my foreach loops rather than dealing with the breaks.

New Code

foreach($name_books as $test) {
    foreach ($test as $key => $value) {
        if(!isset($book_code)){
            $book_code = array_search($row['name'],$test);
            echo $book_code; //just to see if it works, which it does
        }
    } 
}

echo $book_code." is the book code";
0

精彩评论

暂无评论...
验证码 换一张
取 消