开发者

walk through an array from the coordinates (php)

this is my first participation in the list, I'm the following problem, I have a dynamic query that will always me return an array with n elements, like the example below:

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)
      )
)

I need to create a function to analyze the array, comparing the start_points with the end_points. if they elements are equal, the lengths are accumulated into a new array, like this:

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
       )

)

What interests me is the 开发者_StackOverflow中文版river of longest length (river_4), please note that the from coordinates informed, I could compose 5 RIVERS. See the picture: https://picasaweb.google.com/benigno.marcello/Duvida?feat=directlink . The rivers of the array are in yellow (in the watershed). Anyone can help me?

Thanks in advance,


It's a little dirty, but this should work:

// 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');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜