To start with, to illustrate the behavior I desire, look at the following JS code:
let firstObj;
let secondObj;
firstObj = secondObj = {innerAttribute: "the initial value"};
firstObj.innerAttribute = "a new value";
console.log(secondObj.innerAttribute);
This would output "a new value" since firstObj and secondObj are pointing to the same object.
In my application, I am making an ajax call to a PHP script that might look something like this:
<?php
$phpArray = array();
$phpArray["key1"] = $phpArray["key2"] = array("firstValue","secondValue");
json_encode($phpArray);
?>
The problem is that when this array is received in the JS, key1 and key2 aren't pointing to the same object. If I do this in JS with the successfully retrieved object:
retrievedObject["key1"][0] = "firstValueModified";
console.log(retrievedObject["key2"][0]);
It outputs "firstValue", indicating that php created two different objects rather than two pointers to the same object. I tried using the & in PHP like this:
<?php
$phpArray = array();
$phpArray["key1"] = array("firstValue","secondValue");
$phpArray["key2"] = &$phpArray["key1"];
json_encode($phpArray);
?>
But this did not yield the desired behavior either.
These are all very basic examples of what I'm trying to do, as my actual code is much more complex. I just want to know if it's possible to get PHP to return an object to JS with multiple properties or indicies pointing to the same inner object such that a change to one is a change to both.
The practical reason I need this is because I need to import an object where each item has both a numerical and associative reference to it(like what MYSQLI_BOTH does, though it seems to do it by making two different objects), and I need to be able to modify each item in JS via either the numerical or associative reference and have it reflected in the other.
Is what I'm trying to do possible?