title: Searching value in array and comparing / getting next and previous values
body: <p>I have an array <code>$arr</code> looking like this:</p>
  
  <pre><code>array(3) {
    [0]=&gt;
    array(2) {
      ["food"]=&gt;
      string(5) "Toast"
      ["age"]=&gt;
      string(2) "28"
    }
    [1]=&gt;
    array(2) {
      ["food"]=&gt;
      string(5) "Pizza"
      ["age"]=&gt;
      string(2) "45"
    }
    [2]=&gt;
    array(2) {
      ["food"]=&gt;
      string(5) "Steak"
      ["age"]=&gt;
      string(2) "33"
    }
  }
  </code></pre>
  
  <p>I also have a variable <code>$curr_age = 45;</code>.</p>
  
  <p>Now I am looking for a way to search through the array until I find <code>$arr[$i]["age"] = $curr_age</code> and then stop.<br>
  Also, how do I get the food from the next &amp; previous arrays (Toast and Steak in the example)?</p>
  
  <p>For searching through the array, I have this code:</p>
  
  <pre><code>$i = 0;
  
  foreach( $arr as $array ){ 
  
      if( $curr_age == $arr[$i]["age"] ) {
  
          $next = $arr[$i++]["food"];
          $prev = $arr[$i--]["food"];
  
      }
  
      $i++;
  
   }
  </code></pre>
  
  <p>As you can probably imagine, this is not working as expected and I have no idea why. One thing that strikes me though is the fact that the <code>foreach</code> loop is actually only used to increment <code>$i</code> and that <code>$array</code> is never used, but I can't get my head around how to fix this or how to perform the task mentioned above in general.</p>
  
  <p>I think I already found out that there is no "built in" way in PHP to do this, an extra function is needed, but again: I have no clue what that function needs to do. I am not even sure if this statement is true.</p>
  
  <p>I already tried <code>array_search</code> and then simply increment/decrement the key by 1, but that didn't work for two reasons I believe. First it seems that <code>array_search</code> can't handle that type of array &amp; second, the keys can be non-consecutive, so doing <code>$x++</code> or <code>$x--</code> doesn't seem right.</p>