Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $A = array(10, 9, 7, 6, 6, 5, 4, 4, 3, 2);
- $B = array(14, 14, 14, 14);
- echo "<b>A:</b> "; print_r($A);
- echo "<br><b>B:</b> "; print_r($B);
- $arr_result = array();
- $nb = count($B);
- $val = $B[0];
- // utk result 2elemen
- $C = $A;
- $nr = count($arr_result);
- for($i=$nr;$i<$nb;$i++) {
- $x1 = array_shift($C);
- for($j=0;$j<count($C);$j++) {
- $x2 = $C[$j];
- if($x1+$x2==$val) {
- $arr_result["A$i"] = array($x1,$x2);
- array_shift($A);
- array_splice($A,$j,1);
- break;
- }
- }
- }
- // print_r($arr_result);
- // print_r($A);
- // utk result 3elemen
- $C = $A;
- $nr = count($arr_result);
- if($nr < $nb) {
- for($i=$nr;$i<$nb;$i++) {
- $x1 = array_shift($C);
- array_shift($A);
- for($j=0;$j<count($C);$j++) {
- $x2 = $C[$j];
- for($k=$j+1;$k<count($C);$k++) {
- $x3 = $C[$k];
- if($x1+$x2+$x3==$val) {
- $arr_result["A$i"] = array($x1,$x2,$x3);
- array_shift($A);
- array_splice($A,$j,1);
- break;
- }
- }
- }
- }
- }
- echo "<pre>";
- echo "<b>Result:</b> ";
- print_r($arr_result);
- echo "</pre>";
- // print_r($A);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment