1

I'm trying to change values of an array if one of the value is several times into and change by null the duplicate values exept the first.

I have this array: $array =

array (size=6)
  0 => 
    array (size=3)
      'id' => int 1
      'data-time-start' => int 0
      'data-time-end' => int 5
  1 => 
    array (size=3)
      'id' => int 2
      'data-time-start' => int 6
      'data-time-end' => int 10
  2 => 
    array (size=3)
      'id' => int 1
      'data-time-start' => int 11
      'data-time-end' => int 15
  3 => 
    array (size=3)
      'id' => int 3
      'data-time-start' => int 16
      'data-time-end' => int 20
  4 => 
    array (size=3)
      'id' => int 4
      'data-time-start' => int 21
      'data-time-end' => int 25
  5 => 
    array (size=3)
      'id' => int 3
      'data-time-start' => int 30
      'data-time-end' => int 35
  6 => 
    array (size=3)
      'id' =>  null
      'data-time-start' => int 40
      'data-time-end' => int 45
  7 => 
    array (size=3)
      'id' => int 3
      'data-time-start' => int 45
      'data-time-end' => int 60

In this example I have two times 'id' = int 1 and three times 'id' = int 3 . What I would like to do is to keep the first ID but set other duplicate IDs to NULL and return an array like this:

array (size=6)
  0 => 
    array (size=3)
      'id' => int 1
      'data-time-start' => int 0
      'data-time-end' => int 5
  1 => 
    array (size=3)
      'id' => int 2
      'data-time-start' => int 6
      'data-time-end' => int 10
  2 => 
    array (size=3)
      'id' => null
      'data-time-start' => int 11
      'data-time-end' => int 15
  3 => 
    array (size=3)
      'id' => int 3
      'data-time-start' => int 16
      'data-time-end' => int 20
  4 => 
    array (size=3)
      'id' => int 4
      'data-time-start' => int 21
      'data-time-end' => int 25
  5 => 
    array (size=3)
      'id' => null
      'data-time-start' => int 30
      'data-time-end' => int 35
  6 => 
    array (size=3)
      'id' => null
      'data-time-start' => int 40
      'data-time-end' => int 45
  7 => 
    array (size=3)
      'id' => null
      'data-time-start' => int 45
      'data-time-end' => int 60

Tried with that but nothing happened...

var_dump(setNulForDuplicatesInArray($array, 'id'))

function setNulForDuplicatesInArray($arr, $keyToFind)
    {
        $newArr = array();
        for ($i = 0; $i < sizeof($arr); $i++) {
            if (in_array($arr[$i], $newArr))
                $newArr[$i][$keyToFind] = null;
            else
                $newArr[] = $arr[$i];
        }
        return $newArr;
    }

array() is the same...

Thanks !

1 Answer 1

1

One way is to create another container that will be used for checking if that particular ID has already been taken. In every iteration, you'll have to check it. If it has been already inside the container, then set that id to null. Rough example:

$temp = array(); // temporary container
foreach($array as &$a) {
    if(!isset($temp[$a['id']])) { // if this id isn't here yet
        $temp[$a['id']] = $a; // push it inside
    } else {
        $a['id'] = null; // if its already taken, set it to null
    }
}

Sample Output

Sign up to request clarification or add additional context in comments.

4 Comments

Okay for explainations but your function return 'null'
@Zagloo i don't know how you're using this but it works fine codepad.viper-7.com/nQxABn
oh sorry... I put your code in a function and return $temp instead of $array... Thanks for all :) !!
@Zagloo here's an application on your function codepad.viper-7.com/pdz2S5 im glad this helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.