PHP: duplicate value removal

左心房为你撑大大i 提交于 2019-12-04 06:32:54

问题


I have 2 arrays with for example 1000 key's each, one holds a temperature value and the other the hour.

Example array temp:

[0] = 3.1
[1] = 4.3
[2] = 4.1
[3] = 5.1
[4] = 4.1

Example hour array:

[0] = 0
[1] = 1
[2] = 2
[3] = 3
[4] = 3

The problem with this is that when i combine these to arrays and plot this in for example pchart i have too many values on the X and it gets cluttered.
So what i need to to remove the duplicate hours and replace then with "NULL", so that the unneeded hours are not plotted on the x axis.
I want to keep the first hour in the array, the second to the end of the duplicates can be set to "NULL"

The hour output array should be:

[0] = 0
[1] = 1
[2] = 2
[3] = 3
[4] = NULL
etc.

回答1:


Your array is sorted, so... how about this?

$hours = array(0,1,2,3,3,4,4,5);
$prev = -1;
foreach ($hours as &$hour) {
  if ($prev === $hour) {
    $hour = NULL;
  }
  else {
    $prev = $hour;
  }
}
unset($hour);
print_r($hours); // 0,1,2,3,NULL,4,NULL,5...



回答2:


Sounds like a job for array_unique().

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.


If you require the array keys to persist, you can try something with array_map().

<?php

//Variable initialization
$array = array(
    0 => 0,
    1 => 1,
    2 => 2,
    3 => 3,
    4 => 3
);
$temp  = array();

$array = array_map(function($element) use (&$temp) {
    if (!in_array($element, $temp)) {
        $temp[] = $element;
        return $element;
    }
    return null;
}, $array);

print_r($array);



回答3:


If you're using php 5.3:

$a = array(0,1,2,3,4,4,5);

array_walk($a, function(&$item) {
  static $encountered = array();
  if(in_array($item, $encountered)) {
    $item = null;
    return;
  }
  $encountered[] = $item;
});

var_dump($a);

Will preserve the number of keys. Array_walk calls a user function for every key. static makes it so that the $encountered array in the scope of the function stays between executions.




回答4:


If you want to remove the duplicates entirely you can use array_unique() but it wont set them to NULL.




回答5:


Maybe this trick does it:

$simplified = array_combine($hours, $temperatures);

$hours = array_keys($simplified);
$temperatures = array_values($simplified);

This won't set things to NULL but to completely remove them which I think is what you're looking for. Otherwise this should do it:

foreach(array_slice(array_reverse(array_keys($hours)), 0, -1) as $i)
    ($hours[$i] === $hours[$i-1]) && $hours[$i] = NULL;

Demo



来源:https://stackoverflow.com/questions/10435986/php-duplicate-value-removal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!