2

I have an array that stores some Employee Objects ie.

var $this->employeeArray = array();
$this->employeeArray[] = $empObjectA;
$this->employeeArray[] = $empObjectB;
...

which Employee object has id, firstName, lastName etc. I also have a function to search for the employee object that with certain ID. ie:

public function searchArrayByID($id) {
$targetObject = null;

        foreach($this->employeeArray as $e) {
            if ($id == $e->id) {
                $targetObject = $e;
                break;
            }
        }//foreach

return $targetObject;

}

but when I do:

$targetEmployee = $this->searchArrayByID(1);
$targetEmployee->firstName = "someOtherName";

and do a

print_r($this->employeeArray);

that object inside the Array is not being changed.

0

2 Answers 2

2

try this, with the & prepended, it will pass the reference. i also simplified your search function.

Since i dont know why it isnt working for you, because its working for me on 2 different servers without any & i can just suggest the 'safest' method => force references wherever possible

$this->employeeArray[] = &$empObjectA;  // here

public function &searchArrayByID($id) {   // here
    foreach($this->employeeArray as &$e) {   // and here
        if ($id == $e->id) return $e;
    }
    return null;
}

$targetEmployee = $this->searchArrayByID(1);

now if that doesnt work, i suspect another error in your code, because every reference is forced here

funny thing is. i tried it here: http://phpfiddle.org/main/code/2cv-pt2 and with that php version, it makes no difference (thats how it should be). Which php version are you using? Because PHP got better at handling references (reducing unwanted/unnecessary copies)

Sign up to request clarification or add additional context in comments.

8 Comments

unfortunately it's not working. I have used print_r on both $targetEmployee and $this->employeeArray. $targetEmployee contains the changes while the obj in $this->employeeArray doesn't
I'm using php 5.3.15 (mac)
plz tell me the php verion you are using, as i said, old php version are bad with reference, it tends to copy alot. and some manual referencing isnt allowed in newer version, thats why i cant tell what code will work for you
can you run this code: phpfiddle.org/main/code/890-j3a on your server? As you see on phpfiddle, it works fine, with and without the &'s. plz post the output of my code from your server
this is what I got: $obj1: stdClass Object ( [id] => 1 [name] => xyz ) $emp1: stdClass Object ( [id] => 1 [name] => xyz ) $obj2: stdClass Object ( [id] => 2 [name] => zui ) $emp2: stdClass Object ( [id] => 2 [name] => zui ) $empArr: Array ( [0] => stdClass Object ( [id] => 1 [name] => xyz ) [1] => stdClass Object ( [id] => 2 [name] => zui ) ) so when adding object to array I have to put an ampersand like: $this->array[] = &obj?
|
0

That's because PHP copies the object to $targetEmployee, it's not linked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.