Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?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"]);