array_push changes the values

0

link

As you can see in the example array_push or array_unshift save [data] with the last value.

<?php
$filters   = json_decode(strtolower('{"groupOp":"AND","rules":[{"field":"codigo","op":"cn","data":"ampolla 213"},{"field":"codigo","op":"cn","data":"hola rzD"}]}'));
        if (count(explode(' ', preg_replace("/\s+/", " ", $filters->rules[0]->data)))>1) {
            $tmp_array = $filters->rules[0];
            array_shift($filters->rules);

            unset($tmp_array->data);
            foreach(explode(' ', preg_replace("/\s+/", " ", $filters->rules[0]->data)) as &$value) {
                $tmp_array->data = $value;
                print_r($tmp_array);
                array_unshift($filters->rules, $tmp_array);
            }
            print_r($filters);
        }

Output

stdClass Object
(
    [field] => codigo
    [op] => cn
    [data] => hola
)
stdClass Object
(
    [field] => codigo
    [op] => cn
    [data] => rzd
)
stdClass Object
(
    [groupop] => and
    [rules] => Array
        (
            [0] => stdClass Object
                (
                    [field] => codigo
                    [op] => cn
                    [data] => rzd
                )

            [1] => stdClass Object
                (
                    [field] => codigo
                    [op] => cn
                    [data] => rzd
                )
    
asked by John Vargas 27.11.2017 в 13:12
source

2 answers

1

When you change the value of $tmp_array->data you are affecting all the occurrences of that stdClass object contained in the $filters->rules array, because that object that you insert ("prependeas") in the array with array_unshift passes < strong> by reference .

This is in the documentation :

  

As of PHP 5, an object variable does not contain the object itself   value anymore It only contains an object identifier that allows   object accessors to find the current object. When an object is sent by   argument, returned or assigned to another variable, the different   variables are not aliases: they hold a copy of the identifier, which    points to the same object .

This you could try with a simpler example:

<?php

$rules=[];
$objeto=new stdClass();

$objeto->data='valor 0';
$rules[] = $objeto;

$objeto->data='valor 1';
$rules[] = $objeto;

$objeto->data='valor 2';
$rules[] = $objeto;

print_r($rules);

(You can see it in link )

If you would like to modify the value inserted in each loop, you would have to create a new object or clone the previous one:

<?php

$rules=[];
$objeto=new stdClass();

$objeto->data='valor 0';
$rules[] = $objeto;

$objeto = clone $objeto;
$objeto->data='valor 1';
$rules[] = $objeto;

$objeto = clone $objeto;
$objeto->data='valor 2';
$rules[] = $objeto;

print_r($rules);

(See link )

    
answered by 27.11.2017 / 14:10
source
0

Thanks for your comment, I was researching a lot before asking (I had to go through json_enconde (, TRUE) and I did not have these problems.

Greetings.

    
answered by 29.11.2017 в 01:33