PHP Array question, something wrong with how PHP manages references??

Look at the following short code snippet, and please read my remarks. It seems like PHP is handling references in a bizarre way:

     // Create an array
    $arr = array();
    // Make $arr's first element another array, so it's a 2-dimensional array now
    array_push($arr, array());
    // Get a reference to the first element of $arr, which an array
    $arrElement = $arr[0];    
    // Add a first element to the second dimensional array
    array_push($arrElement, "John Doe");
    
    // Why does this not show what I just added???
    var_dump($arr);

    // I have to equate it back into the $arr ...
    $arr[0] = $arrElement;
    // ... for it to show the change!
    var_dump($arr);

Open in new window

elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Arrays do not work by reference.  Objects, however, do work by reference.  There is a special notation for reference, using the ampersand.  It's an artifact of PHP's long history, which predates modern programming practices.  This article explains it.
http://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Here is an example.
http://iconoun.com/demo/temp_elepil.php

<?php // demo/temp_elepil.php
/**
 * http://www.experts-exchange.com/questions/28691109/PHP-Array-question-something-wrong-with-how-PHP-manages-references.html
 */
error_reporting(E_ALL);
echo '<pre>';

// Create an array
$arr = array();

// Make $arr's first element another array, so it's a 2-dimensional array now
array_push($arr, array());
var_dump($arr);

// Get a REFERENCE to the first element of $arr, USING THE AMPERSAND NOTATION
$arrElement = &$arr[0];
// Add a first element (STRING VARIABLE) to the second dimensional array
array_push($arrElement, "John Doe");

// Show the $arr variable
var_dump($arr);

Open in new window

elepilAuthor Commented:
Ray, thanks for responding.

In your statement $arrElement = &$arr[0], why should you have to use the &?? The first element of $arr IS an ARRAY, which is a reference, not a value.

This reference concepts seems to work fine in other scenarios, but just not in this case where a two-dimensional array was involved. This one works as I expected:

    $std = new stdClass();
    $std->name = "John Doe";
    
    $arr = array();
    array_push($arr, $std);
    var_dump($arr); // You will see John Doe
    // Here I modify the name property OUTSIDE of the array ...
    $arrElement = $arr[0];
    $arrElement->name = "Jane Doe";
    // But the array still reflects the change, which is expected.
    var_dump($arr);

Open in new window


The only difference between the above example is that I am using an instance of stdclass instead of another array in my first example. But both are objects. Why does it work for stdclass and not an array? Why do I have to use an &?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

elepilAuthor Commented:
"Arrays do not work by reference". Oh brother. Another reason in the realm fo the bizarre to criticize PHP for. Thanks for your help.
Ray PaseurCommented:
Yeah, this is one of those things that is different from other languages, probably because PHP is older than many of them.  In JavaScript, for example, you would handle arrays and objects in similar ways, however arrays can only have numeric indexes, and the "associative array" in PHP becomes an object in JavaScript.  Inheritance in PHP makes more sense to me than JavaScript; PHP is more classical and javaScript uses the prototype.  It's just another one of those things...
elepilAuthor Commented:
probably because PHP is older than many of them

They should've rewritten PHP from the ground up to make it a true OOP language instead of forcing in the 'plumbing' to make it OOP-like.

PHP's inheritance is just like the classic OOP languages. I still enjoy coding with JavaScript than PHP though because aside from its unorthodox prototypal inheritance, it does behave closer to classic OOP than PHP does, in my humble opinion.
Ray PaseurCommented:
I would agree with that assessment.  And in the case of JavaScript, what choice do we have?  ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.