How do I get a reference to an array in PHP 5?

In php 5, all variable and objects are passed by reference, but I cannot get my codes to work

My codes:

$arrayA = array();

$array = $arrayA;
...
if(!in_array(thedata, $array)
    $array[] = thedata;
var_dump($arrayA);

      

The result is empty, am I missing something simple?

+2


source to share


4 answers


<?php
    $arrayA = array();
    $arrayB =& $arrayA;
    $arrayB = array(1,2,3);
    var_dump($arrayA);

      

More details here:



http://php.net/manual/en/language.types.array.php (link search)

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

+5


source


In PHP5, all objects are passed by reference (more or less), not all variables.



$array =& $arrayA;

      

+1


source


Only objects are passed by reference. If you want to refer to simple types, you must use = & to assign:

php > $var1 = 'xxxxx';
php > $var2 =& $var1;
php > $var1 = 'yyyyy';
php > echo $var2;
yyyyy

      

+1


source


$array =& $arrayA;

      

0


source







All Articles