Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Question about JavaScript, PHP, and passing object references between the two

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It doesn't sound like you need to use references/pointers at all. A pointer does not bind to a data value but rather a location in the memory. It is a mechanism to save memory or produce efficient code.

More simply put, a variable is like a house you can put things in, while a pointer is just the address.

If you have your PHP server pulling data from a MySQL database, and you want that data to be transferred intact to the client browser so it can be processed with JavaScript, then you have to put that data in an object and serialize it into JSON before sending it from the server.

The object could be the mysqli_result object that the MySQLi Database Extension outputs whenever you do a search, or you can create a custom data-holding class:

class dataHolder {
   public int $numerical_reference;
   public string $associative_reference;
   public array $data_row = array();

   public function __construct( int $nr, string $ar, array $data ) {
      $this->numerical_reference = $nr;
      $this->associative_reference = $ar;
      $this->data_row = $data;
   }
}

Then you can use the PHP function serialize to turn that data into a JSON string.

Reference: https://www.php.net/manual/en/function.serialize

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!