0
votes

So im learning about passing by reference, what i dont understand is this, do i need to use pass by reference on functions? Like this

$items = [1,2,3,4,5];
function my_func(&$items) {
    $items[] = 6;
}

    
my_func($items);
var_dump($items);

It will output

array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) }

I have seen people adding values to array by reference like this

class play {
    public function test() {

        $items = [1,2,3,4,5];

        foreach($items as &$item) {
            $item[] = 6;
        }

        return $items;
    }
}

$play = new play;
$play->test();

then i get Warning:

Cannot use a scalar value as an array in

Is there a way in a loop to add a value by reference outside a function?

3
The code you show would be used for a 2-dimensional array, because it's adding something to each row. It doesn't make any sense for a 1-dimensional array. - Barmar
I think what you're looking for is $item = 6;, which will replace every element of the array with 6. - Barmar
If you just want to add a new element to the array, and you're not in a function, you don't need to use a reference variable or a loop. - Barmar
What is the actual result you're trying to get? - Barmar

3 Answers

0
votes

You don't need a function to use reference variables. You can do it with ordinary assignment.

$items = [1,2,3,4,5];
$alias = &$items;

$alias[] = 6;
print_r($items);
0
votes

It seems you misunderstand what the code is doing...

You have an array...

$array = [1,2,3,4,5,6];

...which can be re-written as...

$array = [
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 4,
    4 => 5,
    5 => 6,
];

// Hint: try print_r($array);

...the numbers on the left are the keys and the numbers on the right are the values.

Your loop loops through the array one row at a time:

foreach ($array as $item) {
    echo $item;
}

// Output: 123456

Now, if you add the "pass by reference" operator then you can edit the value of $item; for example we could turn each item in the array to 1...

foreach ($array as &$item) {
    $item = 1;
}

print_r($array);

/* Output:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
)

*/

Without the & in front of $item the code above would only change the value of $item inside of the foreach loop. The original array would not be touched.

The reason that you get an error is because you're assigning a value to $item as though it were an array. Where as, in fact, the $item variable is an integer. So all you need to do to make it work is remove the [] in your assignment line:

$item[] = 6;

// Becomes...

$item = 6;

If, instead of replacing a value you want to add a value then you can do that. But you need to add a value to the array which is $items NOT $item:

$items[] = 6;

Of course, doing that in a loop will add the value once for each item in the array...

0
votes

I figured it out!

$result['books'][0] = ['id' => 1];
$result['books'][1] = ['id' => 2];

foreach ($result['books'] as &$book) {   // use a reference
    $book['new_index'] = 'new value';
    $book['new_book_id'] = '29929292';
}
//unset($book); // unset the reference
echo "<pre>";
print_r($result);
echo "</pre>";