PHP检测非字符串重复项并将其添加到数组及其出现顺序

时间:2013-12-12 06:45:54

标签: php arrays duplicates

我有一个由PHP mysql_fetch_object生成的数组的转储:

Array ( 
[0] => stdClass Object ( [lat] => 44.510728 [lon] => 26.276657 ) 
[1] => stdClass Object ( [lat] => 44.510954 [lon] => 26.277879 ) 
[2] => stdClass Object ( [lat] => 44.510954 [lon] => 26.277879 ) 
[3] => stdClass Object ( [lat] => 44.511655 [lon] => 26.278041 ) 
[4] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 ) 
[5] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 ) 
[6] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 ) 
)

我正在尝试检测重复项并将其添加到与new key->值相同的数组中,并在数组中显示它们的出现顺序,如下所示:

Array ( 
[0] => stdClass Object ( [lat] => 44.510728 [lon] => 26.276657 ) 
[1] => stdClass Object ( [lat] => 44.510954 [lon] => 26.277879 [order] => [0] ) 
[2] => stdClass Object ( [lat] => 44.510954 [lon] => 26.277879 [order] => [1] ) 
[3] => stdClass Object ( [lat] => 44.511655 [lon] => 26.278041 ) 
[4] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 [order] => [0] ) 
[5] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 [order] => [1] ) 
[6] => stdClass Object ( [lat] => 44.510946 [lon] => 26.275215 [order] => [2] ) 
)

谢谢!

1 个答案:

答案 0 :(得分:0)

我随机搜索了这个问题谷歌搜索其他的东西 - 请原谅迟到的回复,但问题没有得到答复,我认为我有一个基于array_keys()的有用答案。

关于array_keys()的一个有趣的事情是,它将可选的第二个值作为搜索值,它检索数组中所有匹配元素的键/索引。

假设数组$objects复制了您的数据集:

$objects = [
    (object) ['lat' => 44.510728, 'lon' => 26.276657 ],
    (object) ['lat' => 44.510954, 'lon' => 26.277879 ],
    (object) ['lat' => 44.510954, 'lon' => 26.277879 ],
    (object) ['lat' => 44.511655, 'lon' => 26.278041 ],
    (object) ['lat' => 44.510946, 'lon' => 26.275215 ],
    (object) ['lat' => 44.510946, 'lon' => 26.275215 ],
    (object) ['lat' => 44.510946, 'lon' => 26.275215 ],
];

如果我们迭代这个对象数组,我们可以检索当前搜索对象的索引以及数组中任何相等的对象;即:具有匹配属性和值的同一类的其他实例。

这样的事情:

foreach ($objects as $object) {

    if (property_exists($object, 'order')) continue;

    $matches = array_keys($objects, $object);

    if (count($matches) > 1) {
        foreach ($matches as $order => $match) {
            $objects[$match]->order = $order;
        }
    }
}

var_dump($objects);

上述代码的快速摘要:

  • 迭代$objects列表。
  • 如果$object已包含order属性,则已处理完毕 - 跳过。
  • 否则,获取数组中所有匹配对象的索引。
  • 对于多个(当前对象)匹配,循环匹配的索引。
  • 将'order'属性应用于所有匹配的对象。

这尚未经过全面测试,但似乎运行良好,也适用于非连续匹配。希望这有帮助!

结果:

array (size=7)
  0 => 
    object(stdClass)[1]
      public 'lat' => float 44.510728
      public 'lon' => float 26.276657
  1 => 
    object(stdClass)[2]
      public 'lat' => float 44.510954
      public 'lon' => float 26.277879
      public 'order' => int 0
  2 => 
    object(stdClass)[3]
      public 'lat' => float 44.510954
      public 'lon' => float 26.277879
      public 'order' => int 1
  3 => 
    object(stdClass)[4]
      public 'lat' => float 44.511655
      public 'lon' => float 26.278041
  4 => 
    object(stdClass)[5]
      public 'lat' => float 44.510946
      public 'lon' => float 26.275215
      public 'order' => int 0
  5 => 
    object(stdClass)[6]
      public 'lat' => float 44.510946
      public 'lon' => float 26.275215
      public 'order' => int 1
  6 => &
    object(stdClass)[7]
      public 'lat' => float 44.510946
      public 'lon' => float 26.275215
      public 'order' => int 2