0

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.
hakre
  • 193,403
  • 52
  • 435
  • 836
HyperDevil
  • 2,519
  • 9
  • 38
  • 52

5 Answers5

3

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);
hakre
  • 193,403
  • 52
  • 435
  • 836
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 1
    Could be, but i need to keep the same amount of keys in both of the arrays. The duplicate keys in the hour array need to be set to NULL, not deleted.. – HyperDevil May 03 '12 at 17:01
  • @HyperDevil: Edited my answer. Will try to figure out a better way than globals. – Madara's Ghost May 03 '12 at 17:08
2

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...
raina77ow
  • 103,633
  • 15
  • 192
  • 229
1

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.

0

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

jarchuleta
  • 1,231
  • 8
  • 10
-2

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

hakre
  • 193,403
  • 52
  • 435
  • 836