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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:07:04+00:00 2026-06-03T22:07:04+00:00

I have an array I’m using as a stack to store a path through

  • 0

I have an array I’m using as a stack to store a path through a tree. Each element points to a node in the tree and I want to pop the last element off and then set the object referred to by that element to null.

Basically:

$node = array_pop($path);
*$node = null;

assuming that PHP had a ‘*’ operator as C-ish languages do. Right now I have the ugly solution of starting at the parent node and remembering which child I took and then setting that to null as in:

if($goLeft) {
    $parent->left = null;
} else {
    $parent->right = null;
}

I say this is ugly because the array containing the path is created by a public function in my tree class. I’d like to expose the ability to work directly on the nodes in a path through the tree without exposing an implementation detail that addresses an idiosyncrasy (feature?) in PHP. ATM I need to include a boolean in the return value ($goLeft in this case) just so I can workaround an inability to dereference a reference.

This is the second time I’ve encountered this problem, so if anyone knows a way I can do something similar to the first block of code please share!

(EDIT)

After experimenting with many permutations of &’s and arrays, it turns out that the basic problem was that I had misinterpreted the reason for an error I was getting.

I tried

$a = ($x > $y) ? &$foo[$bar] : $blah;

and got ” syntax error, unexpected ‘&’ “. I interpreted this to mean that the problem was using the &-operator on $foo[$bar]. It actually turns out that the culprit is the ?-operator, as

if($x > $y) {
    $a = &$foo[$bar];
} else {
    $a = null;
}

works perfectly fine. I thus went on a wild goose chase looking for a workaround for a problem that didn’t exist. As long as I don’t break the chain of &’s, PHP does what I want, which is to operate on the object referred to by a variable (not the variable itself). Example

$a1 = new SomeClass;
$a2 = &$a1;
$a3 = &$a2;
$a4 = &$a3;

$a4 = 42;    // This actually sets $a1 to 42
var_dump($a1); // Emits 42

What messed me up is that I thought objects are passed around by reference anyway (this is wrong), so I didn’t think the & was necessary if the expression resolved to an object. I mean:

class A {
    public $b;
}

class B {}

$a = new A;
$a->b = new B; 

$c1 = $a->b; 
$c2 = &$a->b;

$c1 = 42; // Merely assigns 42 to $c1
$c2 = 42; // Assigns 42 to $a->b

It turns out that this exact issue is addressed at http://www.php.net/manual/en/language.oop5.references.php. Wish that had sunk in the first time I read it!

  • 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-06-03T22:07:06+00:00Added an answer on June 3, 2026 at 10:07 pm

    Very interesting question! I may have found a workaround: if you populate the array with object references, with the & operator, you can destroy the original object by setting that array value to NULL. You have to operate on the array directly, instead of using a variable returned by array_pop. After that you can pop the array to free that position (that would then contain a NULL value).

    This is what I mean (based on Rocket’s code):

    $a=(object)'a';
    $b=array(&$a);
    $b[0] = NULL;
    // array still contains an element
    array_pop($b);
    // now array is empty
    var_dump($a); // NULL
    

    http://codepad.org/3D7Lphde

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

Sidebar

Related Questions

I have array that element is hash a = [{:history_date=>15/07/10}, {:open_price=>7.90}] I want to
I have array data as shown below, I want to store 'sale' value into
Suppose I have array of points.I want to draw the route of that points
I have array values of up[]={0,0,0,0,0} and view=adult thesetwo value i want to store
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have array of class Person in ViewModel and I want to show their
I have array of words and I want to get a hash, where keys
I have array of objects with created images (Object.Image), i want to show images
I have array of objects. I want to write method wich i will use

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.