E
E
entermix2016-06-17 15:14:31
Kohana
entermix, 2016-06-17 15:14:31

Why is the value of the object being reset?

There are sl. the code:

$adverts = DB::select('id', 'rate')
    ->from(ORM::factory('Advert')->table_name())
    ->as_object('Advert')->execute();

foreach ($adverts as $advert){
    $advert->rate = rand(0,10);
    var_dump($advert->rate);
}

foreach ($adverts as $advert)
    var_dump($advert->rate);

As a result:
float(5)
NULL

Why is that? Those. why is the value lost $advert->rate?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2016-06-17
@entermix

// в $advert - копия элемента $adverts, изменения в ней не отражаются на самом массиве
foreach ($adverts as $advert) { } 
// в $advert - ссылка на элемент $adverts, изменения в ней - это изменения в самом массиве
foreach ($adverts as &$advert) { }

From any PHP textbook...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question