Last active
August 29, 2015 14:12
-
-
Save PrestaEdit/24a454051825925590da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
On obtient les données contenues dans le fichier "01. Datas". | |
On ne sait pas si les 4 tableaux (object_type_1, object_type_2, ...) seront ou non remplis. | |
On ne sait pas le nombre de données contenues par tableau. | |
On souhaite combiner les données (critères) pour obtenir l'ensemble des combinaisons possibles en les associant. | |
Exemple illustré de résultat: 03. Combinations | |
L'idée ? Trouver l'algorithme en PHP. |
This file contains hidden or 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
[object_type_1] => array( | |
[0] => 10, | |
[1] => 11 | |
); | |
[object_type_2] => array( | |
[0] => 20, | |
[1] => 21 | |
); | |
[object_type_3] => array( | |
[0] => 30, | |
[1] => 31, | |
[2] => 32 | |
); | |
[object_type_4] => array( | |
[0] => 40 | |
); |
This file contains hidden or 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
2 * 2 * 3 * 1 = 12 |
This file contains hidden or 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
[0] => 30 10 20 40 | |
[1] => 31 10 20 40 | |
[2] => 32 10 20 40 | |
[3] => 30 10 21 40 | |
[4] => 31 10 21 40 | |
[5] => 32 10 21 40 | |
[6] => 30 11 21 40 | |
[7] => 31 11 21 40 | |
[8] => 32 11 21 40 | |
[9] => 30 11 20 40 | |
[10] => 31 11 20 40 | |
[11] => 32 11 20 40 |
This file contains hidden or 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
private function constructConditions($n, $conditions_object_type_1, $conditions_object_type_2, $conditions_object_type_3, $conditions_object_type_4) | |
{ | |
$combinaisons_types = array('object_type_1', 'object_type_2', 'object_type_3', 'object_type_4'); | |
$n_combinaisons = 0; | |
$type_combinaisons = array(); | |
if ($n['object_type_1'] || $n['object_type_2'] || $n['object_type_3'] || $n['object_type_4']) | |
{ | |
$n_combinaisons = 1; | |
if ($n['object_type_1']) | |
{ | |
$n_combinaisons *= $n['object_type_1']; | |
$type_combinaisons['object_type_1'] = $conditions_object_type_1; | |
} | |
if ($n['object_type_2']) | |
{ | |
$n_combinaisons *= $n['object_type_2']; | |
$type_combinaisons['object_type_2'] = $conditions_object_type_2; | |
} | |
if ($n['object_type_3']) | |
{ | |
$n_combinaisons *= $n['object_type_3']; | |
$type_combinaisons['object_type_3'] = $conditions_object_type_3; | |
} | |
if ($n['object_type_4']) | |
{ | |
$n_combinaisons *= $n['object_type_4']; | |
$type_combinaisons['object_type_4'] = $conditions_object_type_4; | |
} | |
} | |
$type_max = 'unknow'; | |
$n_max = 0; | |
if ($n['object_type_1'] > $n_max) | |
{ | |
$type_max = 'object_type_1'; | |
$n_max = $n['object_type_1']; | |
} | |
if ($n['object_type_2'] > $n_max) | |
{ | |
$type_max = 'object_type_2'; | |
$n_max = $n['object_type_2']; | |
} | |
if ($n['object_type_3'] > $n_max) | |
{ | |
$type_max = 'object_type_3'; | |
$n_max = $n['object_type_3']; | |
} | |
if ($n['object_type_4'] > $n_max) | |
{ | |
$type_max = 'object_type_4'; | |
$n_max = $n['object_type_4']; | |
} | |
$combinaisons = array(); | |
if ($n[$type_max]) | |
{ | |
$frequence = $n_combinaisons / $n[$type_max]; | |
foreach ($type_combinaisons[$type_max] as $k => $condition) | |
{ | |
$i = 0; | |
for ($frequence_tmp = 0; $frequence > $frequence_tmp;) | |
{ | |
$combinaisons[$k + ($i * ($n_combinaisons / $frequence))][$type_max] = $condition; | |
$frequence_tmp += 1; | |
$i += 1; | |
} | |
} | |
} | |
$n_cur = 0; | |
foreach ($combinaisons_types as $combinaisons_type) | |
{ | |
if ((int)$n[$combinaisons_type] && $type_max != $combinaisons_type) | |
{ | |
$frequence = $n_combinaisons / $n[$combinaisons_type]; | |
$r = 0; | |
$a = 0; | |
foreach ($type_combinaisons[$combinaisons_type] as $k => $condition) | |
{ | |
$r = $k; | |
$i = 0; | |
for ($frequence_tmp = 0; $frequence > $frequence_tmp;) | |
{ | |
$idx = ($i + ($r * ($frequence / $n[$combinaisons_type]))); | |
if ($n[$combinaisons_type] != $n_cur) | |
$combinaisons[$idx][$combinaisons_type] = $condition; | |
else | |
$combinaisons[$a][$combinaisons_type] = $condition; | |
$frequence_tmp += 1; | |
$i += 1; | |
$a += 1; | |
if ($i == ($frequence / $n[$combinaisons_type])) | |
{ | |
$i = 0; | |
$r += $n_combinaisons / $frequence; | |
} | |
} | |
} | |
if ($n[$combinaisons_type] > $n_cur) | |
$n_cur = $n[$combinaisons_type]; | |
} | |
ksort($combinaisons); | |
} | |
ksort($combinaisons); | |
return $combinaisons; | |
} |
This file contains hidden or 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
private function constructConditions($conditions_array = array()) | |
{ | |
$combinaisons = $this->makeCombos($conditions_array); | |
return $combinaisons; | |
} | |
private function makeCombos($data, &$all = array(), $group = array(), $val = null, $i = 0) | |
{ | |
if (isset($val)) | |
array_push($group, $val); | |
if ($i >= count($data)) | |
array_push($all, $group); | |
else | |
foreach ($data[$i] as $v) | |
$this->makeCombos($data, $all, $group, $v, $i + 1); | |
return $all; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment