Created
September 20, 2016 04:11
-
-
Save teepluss/be483a9bc726b63ab5152ce9b24856b4 to your computer and use it in GitHub Desktop.
Loop Array Multi Dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist ในตำนาน