3

Not being used to php, I'm facing an issue with accessing arrays and its sub data.

$form['signatories']['signatory1'] = array(...);

I must create a "pointer" to the array created in the line above, I expected the following to work:

$cluster = $form['signatories']['signatory1'];

Testing I'm accessing the same "memory space" proves I'm wrong:

$cluster['signatory_name'] = array(...)
// $form['signatories']['signatory1'] has no elements
// $cluster has a sub element

Like cluster is a copy of the array I want it to point to.

How should I proceed? I tried using the "&" reference sign as mentioned in some blog, but that didn't help.

Thanks! J.

1
  • 1
    Can you post the result of a print_r($form); to get things clearer Commented Jun 18, 2012 at 14:10

2 Answers 2

6

By default, assignment in PHP is by value, not by reference, except for objects.

If you want to pass a reference to the original array, you need to create a reference explicitly:

$cluster = &$form['signatories']['signatory1'];

See assigning by reference in the PHP manual.

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

Comments

1

You can use =& to assign by reference:

$cluster =& $form['signatories']['signatory1'];

Effectively this is two operations. The first is &$form['signatories']['signatory1'] which gives you a reference to $form['signatories']['signatory1']. The second is = which obviously assigns the reference to $cluster.

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.