[英]How can I eliminate repeated elements but leaving the last element in the array PHP
Well, I need to be able to eliminate the repeated elements but keeping the last element, that is, eliminate the previous ones and leave the last one.那么,我需要能够消除重复的元素但保留最后一个元素,即消除前面的并留下最后一个。
This is what I have这就是我所拥有的
exists: [
0: {intervention_id: 2, exists: "yes"}
1: {intervention_id: 2, exists: "no"}
2: {intervention_id: 2, exists: "yes"}
3: {intervention_id: 5, exists: "yes"}
4: {intervention_id: 6, exists: "no"}
5: {intervention_id: 12, exists: "yes"}
]
I have to delete the previous ones.我必须删除以前的。
0: {intervention_id: 2, exists: "yes"}
1: {intervention_id: 2, exists: "no"}
and leave this留下这个
2: {intervention_id: 2, exists: "yes"}
And this is what I need这就是我需要的
exists: [
0: {intervention_id: 2, exists: "yes"}
1: {intervention_id: 5, exists: "yes"}
2: {intervention_id: 6, exists: "no"}
3: {intervention_id: 12, exists: "yes"}
]
I need to keep the last one and delete the previous ones我需要保留最后一个并删除以前的
Try this but delete the last one not the previous ones.试试这个,但删除最后一个而不是以前的。
function unique_multidim_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;
}
You could simply override the value of the current element using the same key.您可以使用相同的键简单地覆盖当前元素的值。 In this case, you will always get the last element for each ID在这种情况下,您将始终获得每个 ID 的最后一个元素
function unique_multidim_array($array, $key) {
$uniq = [];
foreach($array as $val) {
$curVal = $val[$key]; // shortcut of the value
$uniq[$curVal] = $val; // override previous value if exists
}
return array_values($uniq); // array_values to re-index array
}
$exists = [
['intervention_id' => 2, 'exists' => 'yes'],
['intervention_id' => 2, 'exists' => 'no'],
['intervention_id' => 2, 'exists' => 'yes'],
['intervention_id' => 5, 'exists' => 'yes'],
['intervention_id' => 6, 'exists' => 'no'],
['intervention_id' => 12, 'exists' => 'yes'],
];
$uniq = unique_multidim_array($exists, 'intervention_id');
print_r($uniq);
Output: Output:
Array(
[0] => Array(
[intervention_id] => 2
[exists] => yes
)
[1] => Array(
[intervention_id] => 5
[exists] => yes
)
[2] => Array(
[intervention_id] => 6
[exists] => no
)
[3] => Array(
[intervention_id] => 12
[exists] => yes
)
)
$data = [
[ 'intervention_id' => 2, 'exists' => 'yes' ],
[ 'intervention_id' => 2, 'exists' => 'no' ],
[ 'intervention_id' => 2, 'exists' => 'yes' ],
[ 'intervention_id' => 5, 'exists' => 'yes' ],
[ 'intervention_id' => 6, 'exists' => 'no' ],
[ 'intervention_id' => 12, 'exists' => 'yes' ]
];
$map = array_column($data, 'exists', 'intervention_id');
array_walk($map, function (&$value, $key) {
$value = [ 'intervention_id' => $key, 'exists' => $value ];
}, ARRAY_FILTER_USE_BOTH);
$result = array_values($map);
print_r($result);
Here is a solution:这是一个解决方案:
<?Php
$exists = [
[ 'intervention_id' => 2, 'exists' => 'yes' ],
[ 'intervention_id' => 2, 'exists' => 'no' ],
[ 'intervention_id' => 2, 'exists' => 'yes' ],
[ 'intervention_id' => 5, 'exists' => 'yes' ],
[ 'intervention_id' => 6, 'exists' => 'no' ],
[ 'intervention_id' => 12, 'exists' => 'yes' ],
[ 'intervention_id' => 12, 'exists' => 'no' ]
];
function unique_multidim_array(array $array) {
$return = array();
foreach($array as $value) {
$return[$value['intervention_id']] = $value;
}
return array_values($return);
}
print_r( unique_multidim_array($exists) );
?>
output: output:
Array
(
[0] => Array
(
[intervention_id] => 2
[exists] => yes
)
[1] => Array
(
[intervention_id] => 5
[exists] => yes
)
[2] => Array
(
[intervention_id] => 6
[exists] => no
)
[3] => Array
(
[intervention_id] => 12
[exists] => no
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.