使用 array_search 函数时数组中的第一个值重复
First value duplicate in array while using array_search function
我正在尝试根据客户端 ID 从二维数组中删除重复项。该脚本删除除第一个值、第一个客户端之外的所有重复项。
我已经尝试了一些其他方法来将 array_search 的结果与不同的值进行比较,尝试使用 !== 和 === 但没有得到有希望的结果。
将非数值作为数组中的第一个插入,可以完美地删除所有内容。
代码如下:
// Build client list
$ClientList = array();
$counter = 0;
foreach ($ClientTrans as $order => $value) {
$ClientId = $ClientTrans[$order]['customer_id'];
if (array_search($ClientId, array_column($ClientList, 0)) == FALSE && is_numeric($ClientId)) {
$ClientList[$counter][] = $ClientId;
$counter += 1;
}
}
最终结果是一个客户端和一个二维数组的求和值。除第一个客户端外,一切正常,它在新构建的客户端列表中多次出现,没有重复。
这是输入数组
Array (
[0] => Array (
[customer_id] => 50245901
[points] => 299
)
[1] => Array (
[customer_id] => 50245907
[points] => 3847
)
[2] => Array (
[customer_id] => 50245908
[points] => 159
)
[3] => Array (
[customer_id] => 50245910
[points] => 3175
)
[4] => Array (
[customer_id] => 50245914
[points] => 641
)
[5] => Array (
[customer_id] => 50245916
[points] => 449
)
[6] => Array (
[customer_id] => 50245921
[points] => 551
)
[7] => Array (
[customer_id] => 50245927
[points] => 0
)
[8] => Array (
[customer_id] => 50245928
[points] => 602
)
[9] => Array (
[customer_id] => 50245929
[points] => 495
)
[10] => Array (
[customer_id] => 50245931
[points] => 539
)
[11] => Array (
[customer_id] => 50245941
[points] => 0
)
[12] => Array (
[customer_id] => 50245901
[points] => 124
)
[13] => Array (
[customer_id] => 50245901
[points] => 512
)
)
所需的输出 - 客户 ID 50245901 未多次出现:
Array (
[0] => Array (
[customer_id] => 50245901
)
[1] => Array (
[customer_id] => 50245907
)
[2] => Array (
[customer_id] => 50245908
)
[3] => Array (
[customer_id] => 50245910
)
[4] => Array (
[customer_id] => 50245914
)
[5] => Array (
[customer_id] => 50245916
)
[6] => Array (
[customer_id] => 50245921
)
[7] => Array (
[customer_id] => 50245927
)
[8] => Array (
[customer_id] => 50245928
)
[9] => Array (
[customer_id] => 50245929
)
[10] => Array (
[customer_id] => 50245931
)
)
您需要像下面这样简化您的 foreach()
:-
$ClientList = array();
foreach ($ClientTrans as $order => $value) {
$ClientList[$value['customer_id']]['customer_id'] = $value['customer_id'];
}
$ClientList = array_values($ClientList);
你可以简单的写代码,
$arr = array_values(array_unique(array_column($arr, 'customer_id')));
$temp = [];
array_walk($arr, function(&$item,$key) use(&$temp){
$temp[]['customer_id'] = $item;
});
print_r($temp);
array_values — Return 数组的所有值
array_unique — 从数组中删除重复值
array_column — Return 来自输入数组中单个列的值
array_walk — 将用户提供的函数应用于数组的每个成员
Demo.
我正在尝试根据客户端 ID 从二维数组中删除重复项。该脚本删除除第一个值、第一个客户端之外的所有重复项。
我已经尝试了一些其他方法来将 array_search 的结果与不同的值进行比较,尝试使用 !== 和 === 但没有得到有希望的结果。
将非数值作为数组中的第一个插入,可以完美地删除所有内容。
代码如下:
// Build client list
$ClientList = array();
$counter = 0;
foreach ($ClientTrans as $order => $value) {
$ClientId = $ClientTrans[$order]['customer_id'];
if (array_search($ClientId, array_column($ClientList, 0)) == FALSE && is_numeric($ClientId)) {
$ClientList[$counter][] = $ClientId;
$counter += 1;
}
}
最终结果是一个客户端和一个二维数组的求和值。除第一个客户端外,一切正常,它在新构建的客户端列表中多次出现,没有重复。
这是输入数组
Array (
[0] => Array (
[customer_id] => 50245901
[points] => 299
)
[1] => Array (
[customer_id] => 50245907
[points] => 3847
)
[2] => Array (
[customer_id] => 50245908
[points] => 159
)
[3] => Array (
[customer_id] => 50245910
[points] => 3175
)
[4] => Array (
[customer_id] => 50245914
[points] => 641
)
[5] => Array (
[customer_id] => 50245916
[points] => 449
)
[6] => Array (
[customer_id] => 50245921
[points] => 551
)
[7] => Array (
[customer_id] => 50245927
[points] => 0
)
[8] => Array (
[customer_id] => 50245928
[points] => 602
)
[9] => Array (
[customer_id] => 50245929
[points] => 495
)
[10] => Array (
[customer_id] => 50245931
[points] => 539
)
[11] => Array (
[customer_id] => 50245941
[points] => 0
)
[12] => Array (
[customer_id] => 50245901
[points] => 124
)
[13] => Array (
[customer_id] => 50245901
[points] => 512
)
)
所需的输出 - 客户 ID 50245901 未多次出现:
Array (
[0] => Array (
[customer_id] => 50245901
)
[1] => Array (
[customer_id] => 50245907
)
[2] => Array (
[customer_id] => 50245908
)
[3] => Array (
[customer_id] => 50245910
)
[4] => Array (
[customer_id] => 50245914
)
[5] => Array (
[customer_id] => 50245916
)
[6] => Array (
[customer_id] => 50245921
)
[7] => Array (
[customer_id] => 50245927
)
[8] => Array (
[customer_id] => 50245928
)
[9] => Array (
[customer_id] => 50245929
)
[10] => Array (
[customer_id] => 50245931
)
)
您需要像下面这样简化您的 foreach()
:-
$ClientList = array();
foreach ($ClientTrans as $order => $value) {
$ClientList[$value['customer_id']]['customer_id'] = $value['customer_id'];
}
$ClientList = array_values($ClientList);
你可以简单的写代码,
$arr = array_values(array_unique(array_column($arr, 'customer_id')));
$temp = [];
array_walk($arr, function(&$item,$key) use(&$temp){
$temp[]['customer_id'] = $item;
});
print_r($temp);
array_values — Return 数组的所有值
array_unique — 从数组中删除重复值
array_column — Return 来自输入数组中单个列的值
array_walk — 将用户提供的函数应用于数组的每个成员
Demo.