為什么要reject()將此集合中的專案轉換為關聯陣列?
>>> collect([1, 2, 'X', 4])->reject('X')->all();
=> [
0 => 1,
1 => 2,
3 => 3,
]
>>>
uj5u.com熱心網友回復:
collect()->reject建立在 之上collect()->filter,而后者又使用array_filter. (Arr::where只是使用 array_filter 回呼的一種簡單方法)
/**
* Run a filter over each of the items.
*
* @param callable|null $callback
* @return static
*/
public function filter(callable $callback = null)
{
if ($callback) {
return new static(Arr::where($this->items, $callback));
}
return new static(array_filter($this->items));
}
如檔案中所述,Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function.如果未指定陣列鍵,則使用默認值 0->n。當它從 array_values() 回傳時,集合顯然不會洗掉索引;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/361740.html
