1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?php
$test_data = array(4, 3, 5, 2, 0, 3, 7, 0, 7, 5); $data_length = count($test_data);
$current_a = $test_data[0]; $current_a = $test_data[1]; $current_min = abs($current_a - $current_b); for($i=1; $i<$data_length - 1; $i++) { for($j=$i + 1; $j<$data_length; $j++) { $temp_a = $test_data[$i]; $temp_b = $test_data[$j]; $diff_min = abs($temp_a - $temp_b); if ($diff_m < $current_min) { $current_min = $diff_min; $current_a = $temp_a; $current_b = $temp_b; } } }
echo $current_a,' - ',$current_b,' = ',$current_min,' << ',implode(", ", $test_data),"\n";
sort($test_data);
$current_a = $test_data[0]; $current_a = $test_data[1]; $current_min = abs($current_a - $current_b); for($i=1; $i<$data_length; $i++) { $temp_a = $test_data[$i]; $temp_b = $test_data[$i-1]; $diff_min = abs($temp_a - $temp_b); if ($diff_m < $current_min) { $current_min = $diff_min; $current_a = $temp_a; $current_b = $temp_b; } }
echo $current_a,' - ',$current_b,' = ',$current_min,' << ',implode(", ", $test_data),"\n"; ?>
|