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

  • Home
  • SEARCH
  • 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 7697497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:01:41+00:00 2026-05-31T22:01:41+00:00

So here’s what I see this code doing: An array is made A loop

  • 0

So here’s what I see this code doing:

  • An array is made
  • A loop iterates 10 times
    • A new array is created
    • A reference to this new array is saved in the first array
  • 10 arrays now reside in the original array with values 0, 1, 2, 3…

What really happens:

  • WTF?

Code:

<?php

header('Content-type: text/plain');

$arrays = array();

foreach(range(0, 10) as $i)
{
    $arr = array();
    $arr[0] = $i;

    $arrays[] = &$arr;
}

print_r($arrays);

Output:

Array
(
    [0] => Array
        (
            [0] => 10
        )

    [1] => Array
        (
            [0] => 10
        )

    [2] => Array
        (
            [0] => 10
        )

    [3] => Array
        (
            [0] => 10
        )

    [4] => Array
        (
            [0] => 10
        )

    [5] => Array
        (
            [0] => 10
        )

    [6] => Array
        (
            [0] => 10
        )

    [7] => Array
        (
            [0] => 10
        )

    [8] => Array
        (
            [0] => 10
        )

    [9] => Array
        (
            [0] => 10
        )

    [10] => Array
        (
            [0] => 10
        )

)

I would like to know exactly why apparently only the 10th array is referred to ten times, instead of every instance of the arrays being referred to one each.

Also if somebody who isn’t just thinking WTF (like me) would like to edit the title, feel free to do so.

  • 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-31T22:01:43+00:00Added an answer on May 31, 2026 at 10:01 pm

    The line

    $arr = array();
    

    does not create a new array but rather assigns an empty array to the already existing reference.
    If you want the variable name to “point” to a different array in memory, you have to unset() (or “disconnect”) it first before assigning an empty array to it:

    foreach(range(0, 10) as $i)
    {
        unset($arr);
        $arr = array();
        $arr[0] = $i;
    
        $arrays[] = &$arr;
    }
    

    This is because the only operations that can make a variable point to something else is the reference assignment (=&) and the unset().

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

Sidebar

Related Questions

Here's a coding problem for those that like this kind of thing. Let's see
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:
Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:

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.