PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Test { public array $checked = []; private bool $checkedAll = false; //private $checkedAll = null; public function __construct($array) { $this->doEachLong(collect($array)); var_export($this->checked); echo "\n---\n"; $this->checked = []; $this->doEachShort(collect($array)); var_export($this->checked); echo "\n---\n"; $this->checked = []; $this->doClassicLoop(collect($array)); var_export($this->checked); echo "\n---\n"; $this->checked = []; $this->doClassicWalk(collect($array)); var_export($this->checked); } public function doEachLong(Illuminate\Support\Collection $c) { $c->each(function ($id) { $this->checked[$id] = $this->checkedAll; }); } public function doEachShort(Illuminate\Support\Collection $c) { $c->each(fn($id) => $this->checked[$id] = $this->checkedAll); } public function doClassicLoop(Illuminate\Support\Collection $c) { foreach ($c as $id) { $this->checked[$id] = $this->checkedAll; } } public function doClassicWalk(Illuminate\Support\Collection $c) { array_walk($c->toArray(), fn($id) => $this->checked[$id] = $this->checkedAll); } } new Test(["4B14Qax6", "MxYRQGBz", "nZ4namx3"]);
Show:  
Copy Clear