How to change value of an object in php

0

I'm passing an object created in a jQuery file to php and getting it by the following code.

$data = $_POST['data'];
$d = json_decode($data);
$user = $d->user;
$season = $d->season;
print_r($d);

What is returning me the following:

stdClass Object
(
    [user] => 1
    [season] => 2016
    [week201548] => stdClass Object
        (
            [bloco] => Microciclo
            [day24112015] => stdClass Object
                (
                    [z1] => 0
                    [z2] => 0
                    [z3] => 0
                    [z4] => 0
                    [z5] => 0
                    [z6] => 0
                    [z7] => 0
                    [terrain] => Terreno
                    [rpe] => 7
                    [elevation] => 1861
                    [fc] => 140
                    [time] => 250
                    [distance] => 86
                    [training] => 

                    [color] => 
                )

            [day25112015] => stdClass Object
                (
                    [z1] => 0
                    [z2] => 0
                    [z3] => 0
                    [z4] => 0
                    [z5] => 0
                    [z6] => 0
                    [z7] => 0
                    [terrain] => Terreno
                    [rpe] => 7
                    [elevation] => 1861
                    [fc] => 140
                    [time] => 250
                    [distance] => 86
                    [training] => 

                    [color] => 
                )

            [day26112015] => stdClass Object
                (

My question is, how do I change only one value of this object? For example, only [elevation] of [day24112015]

    
asked by anonymous 21.11.2015 / 23:20

1 answer

1

When you ran json_decode($data) , you returned an object. To change the attribute of the object the way you want, it is done as follows:

$d->user->season->week201548->day25112015->elevation = 0; // novo valor do atributo do valor elevation

If you had, for example, run json_decode($data, true) (note the second method argument), you would have received an associative array, which you could modify as follows:

$d['user']['season']['week201548']['day25112015']['elevation'] = 0; // novo valor do atributo do valor elevation
    
21.11.2015 / 23:36