I have a class that contains the following:
class TestClass {
protected $items = [];
public function toArray(){
return $this->items;
}
// Items gets set using set(array $items){}
}
In another class I call this:
$tmpList = $list->toArray();
Now if I am not mistaken, this will create a whole new separate array, so if the array in TestClass
has 100k items calling toArray()
will create another array consisting of another 100k items for two arrays totaling 200k items.
Is there a way for me to just return a reference to the items in TestClass
so I have one array with 100k items and $tmpList
just references those items?
If I do this to sort the array, the array doesn't change
$tmpList = $list->toArray();
usort($tmpList, function($a, $b) use ($orderBy, $direction){
if($direction == 'desc'){
return $b->$orderBy > $a->$orderBy;
}elseif($direction == 'asc'){
return $b->$orderBy < $a->$orderBy;
}
});
If I append this after it:
$list->set($tmpList);
The array gets sorted.