我有一个多维的对象数组:
0 =>
array
32281 => object ...
105145 => object ...
165656 => object ...
194124 => object ...
195397 => object ...
205859 => object ...
1 =>
array
32281 => object ...
91504 => object ...
165656 => object ...
194124 => object ...
195397 => object ...
205859 => object ...
3 =>
array
32281 => object ...
105145 => object ...
165656 => object ...
194124 => object ...
195397 => object ...
205859 => object ...
我想像这样从这个数组中删除重复的数组(在这种情况下,我将删除 1 并且只有 0 和 3,因为 0 和 1 是相同的):
0 =>
array
32281 => object ...
105145 => object ...
165656 => object ...
194124 => object ...
195397 => object ...
205859 => object ...
3 =>
array
32281 => object ...
91504 => object ...
165656 => object ...
194124 => object ...
195397 => object ...
205859 => object ...
我已经尝试了很多array_unique、array_keys array_keys_exists的事情,但没有成功......
例如:
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
或
$result = array();
foreach ($array as $key => $value) {
if(!array_key_exists($key,$result))
$result[$key] = $array[$key];
}
这个函数应该做:
function my_unique($array) {
foreach($array as $key => $value) {
foreach($array as $key2 => $value2) {
if($key != $key2 && $value === $value2) {
unset($array[$key]);
}
}
}
return $array;
}
<?php
$arr = array (0 =>
array(
32281 => new stdClass,
105145 => new stdClass,
165656 => new stdClass,
194124 => new stdClass,
195397 => new stdClass,
205859 => new stdClass,
),
1 =>
array(
32281 => new stdClass,
91504 => new stdClass,
165656 => new stdClass,
194124 => new stdClass,
195397 => new stdClass,
205859 => new stdClass,
),
3 =>
array(
32281 => new stdClass,
105145 => new stdClass,
165656 => new stdClass,
194124 => new stdClass,
195397 => new stdClass,
205859 => new stdClass,
),
);
$result = array();
function put($value, $key) {
global $result;
$result[$key] = $value;
}
array_walk_recursive($arr, "put");
var_dump($result);
/**
array(7) {
[32281]=>
object(stdClass)#13 (0) {
}
[105145]=>
object(stdClass)#14 (0) {
}
[165656]=>
object(stdClass)#15 (0) {
}
[194124]=>
object(stdClass)#16 (0) {
}
[195397]=>
object(stdClass)#17 (0) {
}
[205859]=>
object(stdClass)#18 (0) {
}
[91504]=>
object(stdClass)#8 (0) {
}
}
*/
如果我理解您的问题正确,您希望根据所有键删除重复的子数组。
假设基数上有一个从 0 开始的自然数组键:
$keys=array_map("serialize",array_map("array_keys",$arr));
$keys=array_unique($keys);
$result=array();
foreach($keys as $idx=>$not_care)
{
$result[$idx]=$arr[$idx];
}
所以对于
$arr=array(array(1234=>"1234",5678=>"5678"),
array(1357=>"1357",2468=>"2468"),
array(1234=>"1234",5678=>"5678"),
array(1357=>"1357",8642=>"8642"));
你会得到:
Array
(
[0] => Array
(
[1234] => 1234
[5678] => 5678
)
[1] => Array
(
[1357] => 1357
[2468] => 2468
)
[3] => Array
(
[1357] => 1357
[8642] => 8642
)
)
你可以
试试
$array = array(
0 =>
array (
32281 => new stdClass ,
105145 => new stdClass ,
165656 => new stdClass ,
194124 => new stdClass ,
195397 => new stdClass ,
205859 => new stdClass ),
1 =>
array (
32281 => new stdClass ,
91504 => new stdClass ,
165656 => new stdClass ,
194124 => new stdClass ,
195397 => new stdClass ,
205859 => new stdClass ) ,
3 =>
array (
32281 => new stdClass ,
105145 => new stdClass ,
165656 => new stdClass ,
194124 => new stdClass ,
195397 => new stdClass ,
205859 => new stdClass )
);
$array2 = array_unique(array_map("serialize", $array));
$final = array_map("unserialize",array_diff($array2, array_diff_assoc(array_map("serialize", $array), $array2)));
var_dump($final);
输出
array
1 =>
array
32281 =>
object(stdClass)[19]
91504 =>
object(stdClass)[20]
165656 =>
object(stdClass)[21]
194124 =>
object(stdClass)[22]
195397 =>
object(stdClass)[23]
205859 =>
object(stdClass)[24]