justhrun

arraytest.pehape

Aug 19th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2. $A = array(10, 9, 7, 6, 6, 5, 4, 4, 3, 2);
  3. $B = array(14, 14, 14, 14);
  4.  
  5. echo "<b>A:</b> "; print_r($A);
  6. echo "<br><b>B:</b> "; print_r($B);
  7.  
  8. $arr_result = array();
  9. $nb = count($B);
  10. $val = $B[0];
  11.  
  12. // utk result 2elemen
  13. $C = $A;
  14. $nr = count($arr_result);
  15. for($i=$nr;$i<$nb;$i++) {
  16.         $x1 = array_shift($C);
  17.         for($j=0;$j<count($C);$j++) {
  18.                 $x2 = $C[$j];
  19.                 if($x1+$x2==$val) {
  20.                         $arr_result["A$i"] = array($x1,$x2);
  21.                         array_shift($A);
  22.                         array_splice($A,$j,1);
  23.                         break;
  24.                 }
  25.         }
  26. }
  27. // print_r($arr_result);
  28. // print_r($A);
  29.  
  30. // utk result 3elemen
  31. $C = $A;
  32. $nr = count($arr_result);
  33. if($nr < $nb) {
  34.         for($i=$nr;$i<$nb;$i++) {
  35.                 $x1 = array_shift($C);
  36.                 array_shift($A);
  37.                 for($j=0;$j<count($C);$j++) {
  38.                         $x2 = $C[$j];
  39.                         for($k=$j+1;$k<count($C);$k++) {
  40.                                 $x3 = $C[$k];
  41.                                 if($x1+$x2+$x3==$val) {
  42.                                         $arr_result["A$i"] = array($x1,$x2,$x3);
  43.                                         array_shift($A);
  44.                                         array_splice($A,$j,1);
  45.                                         break;
  46.                                 }
  47.                         }
  48.                 }
  49.         }
  50. }
  51.  
  52. echo "<pre>";
  53. echo "<b>Result:</b> ";
  54. print_r($arr_result);
  55. echo "</pre>";
  56. // print_r($A);
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment