2011-05-15 96 views
2

這是我第一次參與列表,我是下面的問題,我有一個動態查詢,將永遠返回一個數組與n個元素,如下面的例子:從座標(php)

 
Array 
(
    [0] => Array 
     (
      [gid] => 311 
      [length] => 408.804653251745 
      [start_point] => POINT(261675 9196115) 
      [end_point] => POINT(261977.5 9196357.5) 
    ) 
    [1] => Array 
     (
      [gid] => 312 
      [length] => 4304.33549546129 
      [start_point] => POINT(259885 9193105) 
      [end_point] => POINT(261675 9196115) 
    ) 
    [2] => Array 
     (
      [gid] => 313 
      [length] => 7470.68109219034 
      [start_point] => POINT(262855 9190095) 
      [end_point] => POINT(261675 9196115) 
    ) 
    [3] => Array 
     (
      [gid] => 314 
      [length] => 1926.81240867132 
      [start_point] => POINT(264465 9190755) 
      [end_point] => POINT(262855 9190095) 
    ) 
    [4] => Array 
     (
      [gid] => 315 
      [length] => 1828.52813742386 
      [start_point] => POINT(264215 9189275) 
      [end_point] => POINT(262855 9190095) 
    ) 
) 

我需要創建一個函數來分析數組,比較start_points和end_points。如果他們元素相等,長度被累積到一個新的數組,像這樣:

 
Array 
(
    [0] => Array 
     (
      [river_1] => "311" 
      [total_length] => 408.804653251745 
     ) 
    [1] => Array 
     (
      [river_2] => "311,312" 
      [total_length] => 4713.140148713 
     ) 
    [2] => Array 
     (
      [river_3] => "311,313" 
      [total_length] => 7879.485745442 
     ) 
    [3] => Array 
     (
      [river_4] => "311,313,314" 
      [total_length] => 9806.298154113 
     ) 
    [4] => Array 
     (
      [river_5] => "311,313,315" 
      [total_length] => 9708.013882866 
     ) 

) 

我感興趣的是最長的長度(river_4)的河流,請注意,從座標通知,我可以譜寫5河流。見圖片:https://picasaweb.google.com/benigno.marcello/Duvida?feat=directlink。陣列的河流呈黃色(在分水嶺中)。任何人都可以幫助我?

由於提前,

回答

2

這是一個有點髒,但是這應該工作:

// Source array (this should already be set) 
$river_data; 

// Results array 
$result_data = array(); 

foreach ($river_data as $river) { 
    // Clear this out just in case 
    $current_river = null; 
    $current_river = array('gid' => $river['gid'], 
        'total_length' => $river['length']); 

    // Compare to everything but ourselves 
    foreach ($river_data as $second_river) { 
     if ($river['end_point'] == $second_river['start_point']) { 
      $current_river['gid'] = $current_river['gid'] . ',' . $second_river['gid']; 
      $current_river['total_length'] = $current_river['total_length'] + $second_river['length']; 
     } 
    } 

    // Add our compound river to the results array 
    $result_data[] = array('river_' . (count($result_data)+1) => $result_data['gid'], 
        'total_length' => $result_data['total_length'); 
}