我已经尝试过各种各样的PHP逻辑和PHP的内置函数来删除重复的值,但它不起作用没有出现错误但是如果使用array_unique() in_array()
删除了所有我的JQuery和CSS都不起作用我的数组中的重复对象
这就是我在while循环中创建person_row_array
的方式:
$person_row = Person::findByID($pi_claimant_row->person_id);
$person_row_array[] = $person_row;
对象print_r如下:
Array
(
[0] => stdClass Object
(
[id] => 12
[flat_no] =>
[house_no] => *
[street] =>
[town] =>
[postcode] => *
[county] =>
)
[1] => stdClass Object
(
[id] => 14
[flat_no] => 2
[house_no] => 33
[street] => Street
[town] => Town
[postcode] => BB
[county] => County
)
[2] => stdClass Object
(
[id] => 14
[flat_no] => 2
[house_no] => 33
[street] => Street
[town] => Town
[postcode] => BB
[county] => County
)
[3] => stdClass Object
(
[id] => 14
[flat_no] => 2
[house_no] => 33
[street] => Street
[town] => Town
[postcode] => BB
[county] => County
)
[4] => stdClass Object
(
[id] => 15
[flat_no] => FN
[house_no] => HN
[street] => Street
[town] => Nelson
[postcode] => PC
[county] => Manchester
)
[5] => stdClass Object
(
[id] => 15
[flat_no] => FN
[house_no] => HN
[street] => Street
[town] => Nelson
[postcode] => PC
[county] => Manchester
)
[6] => stdClass Object
(
[id] => 16
[flat_no] => FN
[house_no] => house
[street] => Manchester Road
[town] => Town
[postcode] => M1 4MK
[county] => County
)
)
我想删除那些重复的请帮助,请不要建议我使用array_unique()我一直在尝试这个过去几个小时没有工作,但如果你能告诉我任何其他方式或添加对象,如果已经没有退出。
请注意这是假数据我只想删除那些具有类似ID的数组。
祝你好运
答案 0 :(得分:7)
尝试以下代码
function my_array_unique($array, $keep_key_assoc = false){
$duplicate_keys = array();
$tmp = array();
foreach ($array as $key => $val){
// convert objects to arrays, in_array() does not support objects
if (is_object($val))
$val = (array)$val;
if (!in_array($val, $tmp))
$tmp[] = $val;
else
$duplicate_keys[] = $key;
}
foreach ($duplicate_keys as $key)
unset($array[$key]);
return $keep_key_assoc ? $array : array_values($array);
}
答案 1 :(得分:2)
您可以使其对于id,name或num之类的任何字段(键)都是唯一的
function unique_multidimensional_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
答案 2 :(得分:0)
这个怎么样?
$dedup = function($array, $key) {
$result = [];
foreach($array as $i) {
if(!isset($result[$i->{$key}])) {
$result[$i->{$key}] = $i;
}
}
// sort($result); <-- Add this if you want to clean up the keys.
return $result;
};
$persons_without_duplicates = $dedup($person_row_array, 'id');