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 8727721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:29:19+00:00 2026-06-13T08:29:19+00:00

I am trying to write a smart array search function that would remember the

  • 0

I am trying to write a “smart” array search function that would remember the last found item.

function &GetShop(&$shops, $id) {
    static $lastShop = null;
    if ($lastShop == null) {
        echo "lastShop is null <br/>";
    } else {
        echo "lastShop: [" . print_r($lastShop, true) . "]<br/>";
    }
    if ($lastShop != null && $lastShop['id'] == $id) {
        return $lastShop;
    }
    for ($i = 0; $i < count($shops); $i++) {
        if ($shops[$i]['id'] == $id) {
            $lastShop = &$shops[$i];
            return $shops[$i];
        }
    }
}

$shops = array(
    array("id"=>"1", "name"=>"bakery"),
    array("id"=>"2", "name"=>"flowers")
);

GetShop($shops, 1);
GetShop($shops, 1);
GetShop($shops, 2);
GetShop($shops, 2);

However, there seems to be an issuer with the line:

$lastShop = &$shops[$i];

When I run this function as it is, I get this output:

lastShop is null 
lastShop is null 
lastShop is null 
lastShop is null 

When I remove the “&” to pass by value instead, it works fine:

lastShop is null 
lastShop: [Array ( [id] => 1 [name] => bakery ) ]
lastShop: [Array ( [id] => 1 [name] => bakery ) ]
lastShop: [Array ( [id] => 2 [name] => flowers ) ]

I would however like to pass by reference because the found array needs to be modified afterwards. Has somebody encountered this issue before and could advice how he solved it?

  • 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-13T08:29:20+00:00Added an answer on June 13, 2026 at 8:29 am

    You are assigning NULL to $lastShop at the beginning of the function block on each call. Thus it is always reset to NULL.

    I found it in the documentation:

    References are not stored statically: […]

    This example demonstrates that when assigning a reference to a static variable, it’s not remembered when you call the &get_instance_ref() function a second time.

    http://php.net/manual/en/language.variables.scope.php#language.variables.scope.references

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that would give me an array of days
I am trying write a function that generates simulated data but if the simulated
I am trying to write a custom function that takes two cell ranges (Start
I'm trying to write a program that can stop and start services using SilverLight
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a function to see how often an object exists and give
I am trying to write a data structure that I can cycle round, sort
I am trying to write a function to get a string from uart. Its

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.