Skip to content

Instantly share code, notes, and snippets.

@teepluss
Created September 20, 2016 04:11
Show Gist options
  • Save teepluss/be483a9bc726b63ab5152ce9b24856b4 to your computer and use it in GitHub Desktop.
Save teepluss/be483a9bc726b63ab5152ce9b24856b4 to your computer and use it in GitHub Desktop.
Loop Array Multi Dimensions
$data = [];
$data[0] = ['S', 'M', 'L'];
$data[1] = ['RED', 'BLACK'];
$data[2] = ['IRON', 'COTTON'];
$data[3] = ['STRONG', 'WEAK'];
$data[4] = ['MAJOR', 'MINOR'];
$data[5] = ['XX', 'YY'];
class Possible
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function k9($arr1, $arr2)
{
$tmp = [];
foreach ($arr1 as $a1) {
foreach ($arr2 as $a2) {
if (count($a1) > 1) {
$tmp[] = array_merge($a1 ,[$a2]);
} else {
$tmp[] = [$a1, $a2];
}
}
}
return $tmp;
}
public function get()
{
$resp = $this->k9($this->data[0], $this->data[1]);
for ($i=2; $i<count($this->data); $i++) {
$resp = $this->k9($resp, $this->data[$i]);
}
return $resp;
}
}
$x = (new Possible($data))->get();
dump($x);
@iamakaradech
Copy link

Gist ในตำนาน

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment