我想知道消除数组中重复项的最佳方法是什么?目前我正在通过一个 foreach 循环来实际获取这个数组,有没有办法说,如果id
已经存在,不要插入到数组中?
foreach($categories2Sugg as $Category2Sugg)
{
$category_stringArray2Sugg[] = array("id"=>$Category2Sugg->id,"name"=>$Category2Sugg->name,"pluralName"=>$Category2Sugg->pluralName,"shortName"=>$Category2Sugg->shortName);
}
Array
(
[0] => Array
(
[id] => 4bf58dd8d48988d16c941735
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
[1] => Array
(
[id] => 4bf58dd8d48988d16c941735
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
[2] => Array
(
[id] => 4bf58dd8d48988d16c941735
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
[3] => Array
(
[id] => 4bf58dd8d48988d14e941735
[name] => American Restaurant
[pluralName] => American Restaurants
[shortName] => American
)
)
或者如果这个数组已经存在,做另一个函数可能更容易,只需删除一些值以输出一个新数组?
谢谢!