Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:51:24+00:00 2026-06-17T04:51:24+00:00

I have an array $arr looking like this: array(3) { [0]=> array(2) { [food]=>

  • 0

I have an array $arr looking like this:

array(3) {
  [0]=>
  array(2) {
    ["food"]=>
    string(5) "Toast"
    ["age"]=>
    string(2) "28"
  }
  [1]=>
  array(2) {
    ["food"]=>
    string(5) "Pizza"
    ["age"]=>
    string(2) "45"
  }
  [2]=>
  array(2) {
    ["food"]=>
    string(5) "Steak"
    ["age"]=>
    string(2) "33"
  }
}

I also have a variable $curr_age = 45;.

Now I am looking for a way to search through the array until I find $arr[$i]["age"] = $curr_age and then stop.
Also, how do I get the food from the next & previous arrays (Toast and Steak in the example)?

For searching through the array, I have this code:

$i = 0;

foreach( $arr as $array ){ 

    if( $curr_age == $arr[$i]["age"] ) {

        $next = $arr[$i++]["food"];
        $prev = $arr[$i--]["food"];

    }

    $i++;

 }

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 foreach loop is actually only used to increment $i and that $array 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.

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.

I already tried array_search and then simply increment/decrement the key by 1, but that didn’t work for two reasons I believe. First it seems that array_search can’t handle that type of array & second, the keys can be non-consecutive, so doing $x++ or $x-- doesn’t seem right.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:51:25+00:00Added an answer on June 17, 2026 at 4:51 am

    Your array indexes are already numeric so there’s not really a need to also maintain an incrementing index, just use the key assignment in the foreach loop, and make sure you test the previous and next keys actually exist before trying to access them

    foreach ($arr as $k => $v) {
        if ($v['age'] == $curr_age) {
            if (isset($arr[$k-1])) {
                $prev = $arr[$k-1];
            }
            if (isset($arr[$k+1])) {
                $next = $arr[$k+1];
            }
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noob question here! I have an array with hashes that looks like this: arr
I have an array named 'names' with strings looking like this: [name_23_something, name_25_something, name_2_something];
I have an array like this var records:Object = {}; var arr:Array = [
If I have an array like this: var arr = ['one','two','three']; I can access
I have an array of integers in string form: var arr = new string[]
I have a list comprehension operating on elements of an .NET array like obj.arr
I have this associative array : Arr = { ID1:{Attr1:b,Attr2:5}, ID2:{Attr1:d,Attr2:2}, ID3:{Attr1:h,Attr2:8} } and
I have one array in javascript like var arr = [aa,bb,cc, dd]; and now
How do I turn this string: house,33;car,43;dog's,99; into this array: arr[33]=house arr[43]=car arr[99]=dog's using
First I have an array like: arr = [[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a],[r,g,b,a]] I can 'flatten' it using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.