Храня объекты в массиве с php

Из Chrome 68:

«Показать метки времени» перенесено в настройки

Ранее флажок «Показать метки времени» в «Настройки консоли» Настройки консоли был перенесен в Настройки .

enter image description here

7
задан Paul Dixon 30 June 2009 в 17:07
поделиться

3 ответа

I would guess that the problem is that you get to the same object every time by reference from the get function and then add it by reference to the array, resulting in all items in the array being modified when the item gets modified in the get function. If that is the case, the following should work:

foreach($query->result() as $content)
{
    $item = $this->{'mod_'.$content->type}->get($content->id);
    print_r($item);
    $items[] = clone $item;
}
print_r($items);
13
ответ дан 6 December 2019 в 09:21
поделиться

When you push $item to $items, it doesn't push the value $item points to but rather the reference itself. You'll need to initialize $item each time:

foreach($query->result() as $content)
{
    $item = new stdClass();
    $item = $this->{'mod_'.$content->type}->get($content->id);
    print_r($item);
    $items[] = $item;
}
print_r($items);
4
ответ дан 6 December 2019 в 09:21
поделиться

Вы, вероятно, возвращаете ссылки на элемент, а не сами элементы. Это всегда будет последняя ссылка, на которую указывает $ item.

3
ответ дан 6 December 2019 в 09:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: