5

I have this php function that has to perform some processing on a given array:

processArray($arrayToProcess) {

  $arrayToProcess['helloStackOverflow'] = TRUE;
}

Later, the code invokes the following:

$niceArray = array('key' => 'value');
processArray($niceArray);

The key 'helloStackOverflow' isn't available outside the processArray function. I tried calling the following:

processArray(&$niceArray);

Using "&" helps, however it raises some warning:

Deprecated function: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of populateForm_withTextfields()

Tried the & in there, but that just stops the code.

How should I do this?

3 Answers 3

18

You have to define the reference in the function, not in the call to the function.

function processArray(&$arrayToProcess) {
Sign up to request clarification or add additional context in comments.

Comments

8
processArray(&$arrayToProcess) {

  $arrayToProcess['helloStackOverflow'] = TRUE;
}

implements the reference in PHP way.

See https://www.php.net/references for useful information about references.

Comments

5
processArray(&$arrayToProcess) {

  $arrayToProcess['helloStackOverflow'] = TRUE;
}

Referencing passing now takes place at the function declaration, not when the function is called.

http://php.net/manual/en/language.references.pass.php

for full documentation.

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.