PHP:通过两个相似的值组合数组元素

时间:2015-04-27 05:55:27

标签: php arrays grouping

我有一个像这样的多维数组:

Array
(
[0] => Array
    (
        [posY] => 5
        [posX] => 48.75
        [id] => 36
    )

 ... 

[5] => Array
    (
        [posY] => 16
        [posX] => 75
        [id] => 15
    )
)

我需要使用类似的posX和posY值对元素进行分组,例如在posX在10到20之间的组中,5到10之间的posY将是元素a(11,9),b(14,8),但不是c(11,15)或d(25,20)。

输出应该是这样的数组:

 Array 
    (
    [1] => Array
        (
            [0] => Array
                (
                    [posY] => 5
                    [posX] => 48.75
                    [id] => 36
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [posY] => 5
                    [posX] => 52.5
                    [id] => 35
                )

            [1] => Array
                (
                    [posY] => 5.5
                    [posX] => 18.75
                    [id] => 46
                )

            [2] => Array
                (
                    [posY] => 7.5
                    [posX] => 52.5
                    [id] => 29
                )
        )

    [3] => Array
        ( 
            [0] => Array
                (
                    [posY] => 15
                    [posX] => 45
                    [id] => 49
                )

             [1] => Array
                (
                    [posY] => 16
                    [posX] => 75
                    [id] => 15
                ) 
        )
    )

通过第一个数组走路的方式是什么,并且使用具有两个相似值的元素组成组?

Tnx很多!

1 个答案:

答案 0 :(得分:0)

一种解决方案可能是计算点之间的距离,并根据笛卡尔接近程度对它们进行分组,而不是“数字接近”。你可以用以下类似的东西来做到这一点(我没有表示这是以任何方式优化的):

  $points = array(
    array('x' => 48.75, 'y' => 5, 'id' => 36),
    array('x' => 52.5, 'y' => 5, 'id' => 35),
    array('x' => 18.75, 'y' => 5.5, 'id' => 46),
    array('x' => 52.5, 'y' => 7.5, 'id' => 20),
    array('x' => 45, 'y' => 15, 'id' => 49),
    array('x' => 75, 'y' => 16, 'id' => 15)
  );

  // calculate the distances from each point, to all the other points.
  foreach($points as &$p)
  {
    foreach($points as $p2)
    {
      if($p == $p2)
        continue;
      $dist = sqrt(pow($p2['x'] - $p['x'], 2) + pow($p2['y'] - $p['y'], 2));
      if(!isset($p['distances']))
        $p['distances'] = array();
      $p['distances'][$p2['id']] = $dist;
    }
  }

  $res = array();
  $used = array();
  $cnt = -1;

  // find points close to each other
  foreach($points as $p)
  {
    $cnt++;
    if(in_array($p['id'], $used))
      continue;
    foreach($points as $p2)
    {
      if(($p['id'] == $p2['id']) || in_array($p2['id'], $used))
        continue;
      if($p['distances'][$p2['id']] < 5) // 5 is arbitrary definition of closeness
      {
        unset($p2['distances']);
        $res[$cnt][] = $p2;
      }
    }
  }
  print_r($res);

此输出是:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [x] => 52.5
                    [y] => 5
                    [id] => 35
                )

            [1] => Array
                (
                    [x] => 52.5
                    [y] => 7.5
                    [id] => 20
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [x] => 48.75
                    [y] => 5
                    [id] => 36
                )

            [1] => Array
                (
                    [x] => 52.5
                    [y] => 7.5
                    [id] => 20
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [x] => 48.75
                    [y] => 5
                    [id] => 36
                )

            [1] => Array
                (
                    [x] => 52.5
                    [y] => 5
                    [id] => 35
                )

        )

)

这不一定会为您提供与CLOSEST元素分组/配对的所有元素,但它会确保所有元素彼此之间具有一定的距离。