一個可能的解決方案保持關鍵信息(我將分配中間結果自己的變量,否則可能會造成混淆閱讀)
$array = array(
143354883 => "1:00am",
144454884 => "12:00am",
145454884 => "12:30am",
144474884 => "1:00am",
144454864 => "1:30am",
143354884 => "1:00am",
144654884 => "1:30am",
1444567584 => "2:00am ",
0 => 4,
1 => 4,
2 => 4,
3 => "Test",
4 => "TEST",
5 => "test "
);
// Used this array_iunique function: http://stackoverflow.com/questions/2276349/case-insensitive-array-unique
function array_iunique($array) {
return array_intersect_key(
$array,
array_unique(array_map("StrToLower",$array))
);
}
$unique = array_iunique($array);
// Then get the difference by key, that will give you all the duplicate values:
$diff_key = array_diff_key($array, $unique);
// Now we have to find the values that are in the $diff_key and the $unique because we also want to have those:
$correspondingValues = array_uintersect($unique, $diff_key, "strcasecmp");
// Then we have to combine the $duplicate values with the $diff_key and preserve the keys:
$result = array_replace($correspondingValues, $diff_key);
var_dump($result);
會導致在:
array(10) {
[143354883]=>
string(6) "1:00am"
[144454864]=>
string(6) "1:30am"
[0]=>
int(4)
[3]=>
string(4) "Test"
[144474884]=>
string(6) "1:00am"
[143354884]=>
string(6) "1:00am"
[144654884]=>
string(6) "1:30am"
[1]=>
int(4)
[2]=>
int(4)
[4]=>
string(4) "TEST"
}
不能有以陣列重複鍵....數組鍵必須是唯一....從[PHP文檔]引用( http://www.php.net/manual/en/language.types.array.php):'如果數組聲明中的多個元素使用同一個鍵,則只有最後一個元素被使用,因爲所有其他元素都被覆蓋。 –
我認爲如果你的源使用utc時間,然後使用php datetime類轉換回你的時間,那麼你不需要處理日光/葉年 – Andrew
我有一個複雜的函數與UTC陣列的一天中的時間安德魯在我的服務器發生此問題的同時顯示用戶的時區。 –