Zagloo
Zagloo

Reputation: 1387

PHP - Change value if same and multiple in array exept the first

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 !

Upvotes: 0

Views: 122

Answers (3)

Eranda
Eranda

Reputation: 868

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

Upvotes: 2

ksbg
ksbg

Reputation: 3284

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.

Upvotes: 1

qiandutianxia
qiandutianxia

Reputation: 80

$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);

Upvotes: 1

Related Questions