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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:44:06+00:00 2026-05-27T06:44:06+00:00

I’ve got three functions, foo , bar and baz , which, from my point

  • 0

I’ve got three functions, foo, bar and baz, which, from my point of view, should produce identical results. However, I’m stuck with a problem that references are shared between recursive function calls.

$array = array(
    'subs' => array(
        'a' => 1,
        'b' => 2,
    ),
);

function foo(&$array, $value, $callAgain = true) {
    $subs =& $array['subs'];
    foreach ($subs as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        foo($copy, $value + 1, false);
    }
}

function bar(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        bar($copy, $value + 1, false);
    }
}

function baz(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as $key => $sub)
        $array['subs'][$key] = $value;
    if ($callAgain) {
        $copy = $array;
        baz($copy, $value + 1, false);
    }
}

foo($array, 3);
var_dump($array);
bar($array, 3);
var_dump($array);
baz($array, 3);
var_dump($array);

This code produces the following results:

array
  'subs' => 
    array
      'a' => int 4
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 3

However, I expect all of them to return 3, 3, because copies of the array are passed to recursive calls.

How to fix the first two functions to make them return 3, 3? I’d prefer not to use syntax of the baz function, because it’s very verbose.

  • 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-05-27T06:44:06+00:00Added an answer on May 27, 2026 at 6:44 am

    I think you have answered your own question– baz IS the way to get the behavior you want. The other two functions are behaving as intended in PHP, at least according to the manual page on What References Do:

    Note, however, that references inside arrays are potentially dangerous. Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments. This also applies to function calls where the array is passed by value. Example:

    /* Assignment of array variables */
    $arr = array(1);
    $a =& $arr[0]; //$a and $arr[0] are in the same reference set
    $arr2 = $arr; //not an assignment-by-reference!
    $arr2[0]++;
    /* $a == 2, $arr == array(2) */
    /* The contents of $arr are changed even though it's not a reference! */
    ?>
    

    In other words, the reference behavior of arrays is defined in an element-by-element basis; the reference behavior of individual elements is dissociated from the reference status of the array container.

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

Sidebar

Related Questions

In a simple app I'm working on, I've got three related assemblies: MyCompany.Geography.Windows.Forms which
I've got a shared library with some homemade functions, which I compile into my
I've got a string like "foo%20bar" and I want "foo bar" out of it.
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
I've got three files on AIX that I need to import into a SQL
I've got three boolean values A, B and C. I need to write an
I've got three entities in my application so far: tag, feed and story A
I've got three columns inside of a div that is the full page width.
I've got a CAkePHP 1.2 site. I've got three related Models/tables: A Comment has
In Unix, I have got three main files. One of them is a library

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.