gpt4 book ai didi

PHP返回对其他类中数组的引用

转载 作者:行者123 更新时间:2023-12-01 22:25:23 24 4
gpt4 key购买 nike

<分区>

我有一个包含以下内容的类:

class TestClass {
protected $items = [];

public function toArray(){
return $this->items;
}

// Items gets set using set(array $items){}
}

在另一个类中我称之为:

$tmpList = $list->toArray();

现在,如果我没记错的话,这将创建一个全新的单独数组,因此如果 TestClass 中的数组有 100k 个项目,调用 toArray() 将创建另一个数组由两个数组的另外 100k 项组成,总计 200k 项。

有没有一种方法可以让我只返回对 TestClass 中项目的引用,这样我就有一个包含 100k 项目的数组,而 $tmpList 只引用这些项目?

如果我这样做是为了对数组进行排序,则数组不会改变

$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;
}
});

如果我在它后面附加这个:

$list->set($tmpList);

数组被排序。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com