我試圖改變數組的值,如果一個af值是幾次進入並更改重複值首先豁免。PHP - 更改值,如果相同和多個陣列免除第一個
我有這樣的陣列:
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
在這個例子中我有兩個次'id' = int 1
和三次'id' = int 3
。 我想什麼做的是保持第一ID,但設置其他重複的ID來NULL
,並返回一個這樣的數組:
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
我不是在PHP的專家...
編輯1:
試過是:
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'))
但沒有發生
感謝您的幫助!
爲什麼不通過'的var_dump()'? – D4V1D 2015-04-02 07:42:59
@anantkumarsingh非常不好的建議,如果使用'var_dump'而不是'print_r',你會驚訝地發現這裏有多少問題會很明顯。 – jeroen 2015-04-02 07:46:39