0

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

I have this 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' => int 3
      'data-time-start' => int 40
      'data-time-end' => int 45

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

I'm not an expert in php ...

EDIT 1 :

Tried with that:

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;
    }

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

But nothing happened

Thanks for your help !

2
  • 1
    Why not through var_dump()? Commented Apr 2, 2015 at 7:42
  • 1
    @anantkumarsingh Pretty bad advice, you'd be amazed how many problems here would have been obvious right away if a var_dump had been used instead of a print_r. Commented Apr 2, 2015 at 7:46

3 Answers 3

2
function setNulForDuplicates($theArray) {
    $myarray = array();
    for ($ind = 0; $ind < count($theArray); $ind++) {
        if (in_array($theArray[$ind], $myarray)) {
            $myarray[$ind]['id'] = 'null';
        } else {
            $myarray[$ind] = $theArray[$ind];
        }
    }
    return $myarray;
}

var_dump(setNulForDuplicates($arr));
Sign up to request clarification or add additional context in comments.

1 Comment

Works nice ! but How to do if my array have other index ? see my post, I updated it ! thanks :)
1

Here's a little function that should do the trick:

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

It loops through the array and checks whether the same entry already exists in $newArr. If it does, id is set to null. If not, the entry is being pushed into $newArr.

Please note that in this example null is a string. I did that because in your desired result, null is a string. If that's not desired, simply remove the surrounding quotes.

2 Comments

Works nice ! but How to do if my array have other index like : array (size=6) 0 => array (size=1) 'id' => int 1 1 => array (size=1) 'id' => int 2 2 => array (size=1) 'id' => int 2 3 => array (size=1) 'id' => int 3 ? I update my post
I updated my post because array change a litlle... his got some index more
1
$arr=array(
    array("id"=>1),
    array("id"=>2),
    array("id"=>1),
    array("id"=>3),
    array("id"=>4),
    array("id"=>3),
    array("id"=>3)
    );
var_dump($arr);
$tmp=array();
foreach($arr as $k =>$v){
    if(!in_array($v['id'], $tmp)){
        $tmp[]=$v['id'];
    }else{
        $arr[$k]['id']=null;
    }
}
var_dump($arr);

3 Comments

Works nice ! but How to do if my array have other index ? see my post, I updated it ! thanks :)
Why don't you use print_r
Do not understand it for your post. For example

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.