0

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.

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.

3 Answers 3

5
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did not know break accepted a parameter; it looks like I learned something new today.
This is also news to me. Good post.
2

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.

3 Comments

You type a lot faster than I do and got your answer in first.
thanks for this, i had never used anything but the default break before. However, I forgot that I hadn't included the whole picture!! (sorry!) see my edit above.
That was it, thanks! I'm still a little confused as to why this worked while mine didn't - I'll study it a little longer. But thanks so much for the help.
1

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";

1 Comment

I disagree. Not breaking out of both loops means that it has to finish iterating through the arrays unnecessarily. While realistically, the efficiency is not going to be noticeable unless the arrays are very large, breaking out of the loops is by far more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.