我的数组看起来像这样,
Array
(
[0] => Array
(
[id] => 224983
[name] => James
[weight] => 0
[bank] => Bank A
[transaction] => 1
[total] => 7682000000
[reference] => Edward
[type] => BRANCH
[reference_id] => 222818
[account_number] => 1220007355285
)
[1] => Array
(
[id] => 224984
[name] => James
[weight] => 0
[bank] => Bank A
[transaction] => 1
[total] => 7682000000
[reference] => Edward
[type] => BRANCH
[reference_id] => 222819
[account_number] => 1220007355285
)
[3] => Array
(
[id] => 224985
[name] => Maria
[weight] => 0
[bank] => Bank B
[transaction] => 1
[total] => 1500000000
[reference] => Andrey
[type] => BRANCH
[reference_id] => 247620
[account_number] => 1220000412901
)
)
当account_number,reference和name相同时,我想删除另一个,并根据id保留最后一个...
请有人帮我查一下,我已经被困在这里,所以输出将是[' id] =>删除数组[0] 224983,其余数组将是结果
答案 0 :(得分:0)
尝试以下功能,这将有助于删除重复的数组。
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
答案 1 :(得分:0)
如果您只是想比较数组中的三个字段,那么您可以尝试以下方式。哪些检查重复和未设置以前的条目。这里$ data是数组输入
foreach ($data as $key => $row) {
$id = $row['id'];
$name = $row['name'];
$reference = $row['reference'];
$flag = 0;
for($i = $key + 1; $i < count($data); $i++)
{
if(($data[$i]['id'] == $id) && ($data[$i]['name'] == $name) && ($data[$i]['reference'] == $reference))
{
if($key != $i)
unset($data[$key]);
}
}
}
结果将是
Array
(
[1] => Array
(
[id] => 224983
[name] => James
[weight] => 0
[bank] => Bank A
[transaction] => 1
[total] => 7682000000
[reference] => Edward
[type] => BRANCH
[reference_id] => 222818
[account_number] => 1220007355285
)
[2] => Array
(
[id] => 224985
[name] => Maria
[weight] => 0
[bank] => Bank B
[transaction] => 1
[total] => 1500000000
[reference] => Andrey
[type] => BRANCH
[reference_id] => 247620
[account_number] => 1220000412901
)
)
答案 2 :(得分:0)
它似乎不优雅,但我认为您需要遍历以前的数组元素来检查ID。你可以创建一个像这样的函数:
function id_exists_prior(&$my_array, $array_index) {
for($i = 0; $i < $array_index; $i++) {
if($my_array[$i]['id'] === $my_array[$array_index]['id']) return false;
}
return true;
}
然后你可以在你想要的任何数组元素上调用它,或者循环遍历整个数组以检查重复项。后者的一个例子:
$your_array = [/*Your array data*/];
for($key = 0; $key < count($your_array); $key++) {
if(id_exists_prior($your_array, $key)) //Do your thing
}
将数组键传递给函数的原因是,函数只检查先前的重复项。
希望这有帮助!