Viewing the array by reference changes it (no action has been taken)

edit: do not read the related topic, the answer below is clear and gives a solution, while the other topic simply states the problem.

I have something strange here

My code is as follows:

        var_dump($resultFlatTree);
        foreach($resultFlatTree as &$element)
        {
            /*if(isset($element["action"]) && $element["action"] == "new")
            {
                //let save the original ID so we can find the children
                $originalID = $element["id"];
                //now we get the object
                $newObject = $setUpForDimension->createAnObject($dimension,$element,$customer);
                $element['id'] = $newObject->getId();
                echo "new";
                //and let not forget to change the parent_id of its children
                $arrayFunctions->arrayChangingValues($resultFlatTree,"parent_id",$element['id'],$originalID);
                $em->persist($newObject);                                                             
            } */               
        }            
        $em->flush();
        var_dump($resultFlatTree);

The code inside the foreach is commented out to be sure that this is not what I am doing by modifying the array.

here is the array before foreach:

array(3) {
  [0]=>
  array(10) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(7) "Revenue"
    ["code"]=>
    string(6) "700000"
    ["sense"]=>
    string(2) "CR"
    ["lft"]=>
    int(1)
    ["lvl"]=>
    int(2)
    ["rgt"]=>
    int(1)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00D"
    ["parent_id"]=>
    int(1)
  }
  [1]=>
  array(10) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(7) "Charges"
    ["code"]=>
    string(6) "600000"
    ["sense"]=>
    string(2) "DR"
    ["lft"]=>
    int(3)
    ["lvl"]=>
    int(2)
    ["rgt"]=>
    int(4)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00P"
    ["parent_id"]=>
    int(4)
  }
  [2]=>
  array(10) {
    ["id"]=>
    int(4)
    ["name"]=>
    string(6) "Energy"
    ["code"]=>
    string(6) "606000"
    ["sense"]=>
    string(2) "DR"
    ["lft"]=>
    int(2)
    ["lvl"]=>
    int(1)
    ["rgt"]=>
    int(5)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00E"
    ["parent_id"]=>
    int(1)
  }
}

and then after:

array(3) {
  [0]=>
  array(10) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(7) "Revenue"
    ["code"]=>
    string(6) "700000"
    ["sense"]=>
    string(2) "CR"
    ["lft"]=>
    int(1)
    ["lvl"]=>
    int(2)
    ["rgt"]=>
    int(1)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00D"
    ["parent_id"]=>
    int(1)
  }
  [1]=>
  array(10) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(7) "Charges"
    ["code"]=>
    string(6) "600000"
    ["sense"]=>
    string(2) "DR"
    ["lft"]=>
    int(3)
    ["lvl"]=>
    int(2)
    ["rgt"]=>
    int(4)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00P"
    ["parent_id"]=>
    int(4)
  }
  [2]=>
  &array(10) {
    ["id"]=>
    int(4)
    ["name"]=>
    string(6) "Energy"
    ["code"]=>
    string(6) "606000"
    ["sense"]=>
    string(2) "DR"
    ["lft"]=>
    int(2)
    ["lvl"]=>
    int(1)
    ["rgt"]=>
    int(5)
    ["root"]=>
    int(1)
    ["$$hashKey"]=>
    string(3) "00E"
    ["parent_id"]=>
    int(1)
  }
}

As you can see, the last element is now changed by reference. This completely destroys the processes that I do with the array afterwards.

Is this normal behavior? How can i avoid this?

+4
source share
1 answer

foreach, :)

http://php.net/manual/en/control-structures.foreach.php

, $value &. .

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>

$ foreach. unset().

, , ref, - .

40 :

" $ foreach. unset()."

! , :

<?php
$arr1 = array("a" => 1, "b" => 2, "c" => 3);
$arr2 = array("x" => 4, "y" => 5, "z" => 6);

foreach ($arr1 as $key => &$val) {}
foreach ($arr2 as $key => $val) {}

var_dump($arr1);
var_dump($arr2);
?>

:

array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6) }
array(3) { ["x"]=> int(4) ["y"]=> int(5) ["z"]=> int(6) }

, $arr1 $arr2!

, , .

TL;DR: /// . , , .

+3

All Articles