所以我試圖將來自兩個獨立API的提要合併爲一個,但首先我需要確保提要包含相同的密鑰,以便我可以將兩者合併在一起。在每個循環中,我存儲了我希望爲每個數組保留的值,但是,我希望每個值都具有唯一的密鑰,而不是經典的[0],[1],[2]等。編號系統。我將如何獲得$ value3爲其擁有的每個值賦予一個唯一的字符串鍵?如何使用foreach循環將現有數組的鍵更改爲唯一值?
$googleArray = array();
foreach ($myEvents as $key => $value) {
foreach ($value as $key2 => $value2) {
// Remove unnecessary keys from google API arrays
unset($value2[method], $value2[kind], $value2[status], $value2[htmlLink], $value2[created], $value2[updated], $value2[colorId], $value2[creator],$value2[organizer],$value2[iCalUID],$value2[sequence],$value2[reminders], $value2[etag],$value2[id],$value2[minutes]);
//Make new keys for googleArray in order to store each list to its designated event
$googleArray[$key2] = $value2;
// foreach ($value2 as $key3[summary] => $value3) {
// Store desired values in new array
// $googleArray[] = $value3;
// }
}
}
基本上,我試圖讓
$key2["summary"] = $key2["DESC1"];
摘要關鍵成爲關鍵DESC1
無關你的問題,你需要設置'的error_reporting(E_ALL)'在你的腳本的頂部或[在你的PHP配置(http://php.net/manual/ en/errorfunc.configuration.php)。當你這樣做時,你會看到例如'$ value2 [kind]',會拋出一個錯誤,因爲當沒有這些名字的常量時,你試圖使用一個常量('kind')。 PHP爲你糾正了這個問題,但它會讓你的應用程序的性能受到很大影響。將'error_reporting'級別增加到'E_ALL'可以讓你看到所有的錯誤並更正你的代碼(在這種情況下,使用'$ value2 ['kind']'代替)。 – 2014-09-26 16:58:22