简体   繁体   English

如何消除重复元素但保留数组中的最后一个元素 PHP

[英]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
        )
)

live demo现场演示

$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.

相关问题 如何获取数组中的下几个元素,但在传递最后一个元素时跳回到开头? - How can I get next few elements in an array but jump back to the start when passing the last element? 如何消除 JavaScript 中的重复元素(包括自身)? - How can I eliminate repeating elements (including itself) in JavaScript? 如果在一个单词中重复某个特定字符,我如何使用 .split 来消除它? - How can I use .split to eliminate a specific character if this are repeated in one word? 如何过滤数组中的重复元素而不用lodash删除它们? - How can I filter repeated elements in array without deleting them with lodash? setTimeout()跳到数组的最后一个元素如何防止这种情况? - setTimeout() jumps to last element of array how can i prevent this? 如何将结果字符串添加到数组中并消除重复项? - How can I add the resulted strings into an array and eliminate duplicates? 如何在数组中找到2个元素并以PHP形式返回数组? - How can I find 2 elements in an array and return the array in a form in PHP? 如何通过Javascript消除多余的,重复的HTML标签? - How can one eliminate redundant, repeated HTML tags via Javascript? 如何在 HTML 中的元素中设置所有数组元素? - How can I set all array elements in an element in HTML? 如何为数组中的第一个元素返回属性值,如果没有元素则返回0? - How can I return a property value for the first element in an array or 0 if there are no elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM