Une idée ? :)Si personne ne répond c'est que soit on ne sait pas, soit on n'a pas le temps. Les "UP" de sujet ne servent malheureusement à rien....
<?php recherche_valeur(array(12.51, 45.62, 9.14, 1, 45, 0.10), 10.14); function recherche_valeur($tab, $result) { $tab_count = count($tab); for ($i = 0 ; $i < $tab_count -1 ; $i++) { for ($j = $i + 1 ; $j < $tab_count ; $j++) { if ($tab[$i] + $tab[$j] == $result) { echo "nombre : " . $tab[$i] . " et " . $tab[$j] . "<br/>"; } } } } ?>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question<?php recherche_valeur(array(12.51, 45.62, 9.14, 1, 45, 0.10), 10.14); function recherche_valeur($tab, $result) { for ($i = 0 ; $i < count($tab) ; $i++) { if (in_array($result - $tab[$i], $tab)) { echo "nombre : " . $tab[$i] . " et " . ($result - $tab[$i]); break; } } } ?>
recherche_valeur(array(91.30, 46.30, 60, 45.30, 89.30,65.10), 260.70); ( Obtenir 260.70 avec les nombres 46.30, 60, 89.30 et 65.10 )
$arr = array(12.51, 45.62, 9.14, 1, 45, 0.10); // compte le nombre d'éléménts $n = count($arr); // declare le tableau de sortie $sortie = array(); // Tableau de sortie // Liste des combinaisons : for($i=1;$i<=$n;$i++) { liste_combinaison(",",$arr,$i); } echo '<pre>'; Echo 'Nb Combinaisons : ' . count($sortie); echo '</pre>'; echo '<pre>'; //print_r($sortie); echo '</pre>'; $tbl_liste_somme=sommeTbl($sortie); $tbl_liste_somme= msort($tbl_liste_somme, array('somme','combinaison')); echo "<table> <tr><th>COMBINAISON</th><th>SOMME</th> </tr>"; for ($l=0;$l<count($tbl_liste_somme);$l++){ echo "<tr>"; Echo "<td>".$tbl_liste_somme[$l]['combinaison']."</td><td>" . $tbl_liste_somme[$l]['somme'] . "</td>"; echo "</tr>"; } echo "</table>"; function liste_combinaison($debut,$tags,$profondeur) { global $sortie; if($profondeur == 0) { array_push($sortie,$debut); return; } $n = count($tags); for($i=0;$i<$n;$i++) { liste_combinaison($debut.$tags[$i].",",array_slice($tags,$i+1),$profondeur-1); } } function Factorielle($number){ $retour = 1; if ($number>1) { $retour = Factorielle($number-1); } return $number*$retour; } function sommeTbl($arr){ // Calcule la somme de chaque combinaison $n = count($arr); $tbl_somme = array(); for($i=0;$i<=$n;$i++) { //Combinaison $tbl_somme[$i]['combinaison'] = substr(substr($arr[$i],1),0,-1); //Somme $explode_val=explode(",",$arr[$i]); $sum = array_sum($explode_val); $tbl_somme[$i]['somme'] = $sum; } return $tbl_somme; } function msort($array, $key, $sort_flags = SORT_REGULAR) { // Trouvé sur : http://blog.jachim.be/2009/09/php-msort-multidimensional-array-sort/ if (is_array($array) && count($array) > 0) { if (!empty($key)) { $mapping = array(); foreach ($array as $k => $v) { $sort_key = ''; if (!is_array($key)) { $sort_key = $v[$key]; } else { // @TODO This should be fixed, now it will be sorted as string foreach ($key as $key_key) { $sort_key .= $v[$key_key]; } $sort_flags = SORT_NUMERIC; } $mapping[$k] = $sort_key; } asort($mapping, $sort_flags); $sorted = array(); foreach ($mapping as $k => $v) { $sorted[] = $array[$k]; } return $sorted; } } return $array; }
Array ( [0] => 12.51 [1] => 45.62 ) Nb Combinaisons : 3 COMBINAISON SOMME 12.51 12.51 45.62 45.62 12.51,45.62 58.13 0