0

In the following array I would like to search for firstname, e.g tom and then echo out the name of the key "library" and "canteen" because there is stored a person with first name tom in both of them.

This is the output of print_r($school);

Array
(
    [library] => Array
         (
            [0] => Array
                (
                    [firstname] => tom
                    [lastname] => brown
                )
        )
)
Array
(
    [canteen] => Array
        (
            [0] => Array
                (
                    [firstname] => matt
                    [lastname] => smith

                )
            [1] => Array
                (
                    [firstname] => tom
                    [lastname] => jones
                )
        )
)

I've done multiple tries with foreach loops without success. I must admit that I'm not completely familiar with the way they work.

This is what I've tried:

foreach ($school as $k => $v) {
    if ($v['firstname'] == 'tom'){
        echo 'Currently at the '.$k.'<br>';
    }
}

This is the output is whish for:

Currently at the library
Currently at the canteen
2
  • You're missing an array level. $arr['canteen'][1]['lastname'] => jones Commented Feb 20, 2014 at 20:31
  • Can you please accept an answer, @user2664370? I see you are a new member(I am new too). You should accept an answer if it solved your problem. Commented Feb 20, 2014 at 22:44

4 Answers 4

0

The problem is you are only iterating trough the first dimension, but you array is twodimensional. Here is the fixed code:

foreach ($school as $k => $va) { //Iterate trough the array $school($k is the key and $va is the value)
 foreach($va as $v) { //Iterate trough $va, this is a multidimensional array. $v is the value.
  if ($v['firstname'] == 'tom'){ 
   echo 'Currently at the '.$k; //$k is still storing the key from the first foreach.
  }
 }
}

Foreach is not hard, but you will need to understand it first. Here is "what the computer says" hen running the code: Ok, Iterate trough the array $school. Now I will store the key library in $k, and value array(...) in $va.
Oh, another foreach... Now I should loop through $va(the value of $school['library']). I should set the $v['firstname'] to 'tom' and the lastname to 'brown'.
The check is ok, echo the key 'library'.
Foreach exits, no more entry in 'library'.
The first foreach is still alive: It should set the $k to canteen and $va to array(...).
Starting new foreach, iterating trough $va($school['canteen']), put key 'canteen' to $k...
$v is now Array("firstname" => "matt", "lastname" => "smith");
Firstname is not 'tom'. Next value from school[canteen].
$v is now Array("firstname" => "tom", "lastname" => "jones");
Firstname is tom, echoing key from $k, it is 'canteen'.
End of array, both foreach exits.

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

1 Comment

I really appreciate your help! It worked just fine. It totally makes sense thanks to your explanation. Too bad I cannot vote yet :(
0

if $school is the outer array then you can do like this:

foreach ($school as $k => $v) {
 foreach($v as $key=>$value{
if ($value['firstname'] == 'tom'){
    echo 'Currently at the '.$key;
}
}
}

Comments

0

You're using multidimensional array. Therefore, you need to use two foreach loops to loop through each array.

Comments

0

You'll need another foreach loop

foreach ($school as $k => $v) {
    foreach($v as $key => $value){
        if ($value['firstname'] == 'tom'){
            echo 'Currently at the ' . $k;
        }
    }
}

Comments

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.