2012-08-02 91 views
-1

我定義我的數組如何將項目添加到我的關聯數組中?

$data = array();

增加1項:

$data['response'] = true;

現在我想在添加多個項目曾經這樣:

$data["picUpload" => $pPicUpload, "Album1" => $Album1, "Album2" => $Album2];

但我得到這個錯誤:

parse error, expecting `']'' 

我在做什麼錯?

+0

$ data = array(「picUpload」=> $ pPicUpload,「Album1」=> $ Album1,「Album2」=> $ Album2); – Debugger 2012-08-02 07:12:19

回答

5

你可以把它寫在多條線路,如:

$data['new0'] = 'value0'; 
$data['new1'] = 'value1'; 

或使用array_merge(將覆蓋碰撞鍵):

$data = array_merge($data, array('new0' => 'value0', 'new1' => 'value1')); 
3

您需要通過一個加一個,或者使用array_merge方法。

$data["picUpload"] =$pPicUpload; 
$data["Album1"] = $Album1; 
$data["Album2"] = $Album2; 
2

您使用的語法不正確。您需要使用array_merge功能

$data = array_merge($data, array(
    "picUpload" => $pPicUpload, 
    "Album1" => $Album1, 
    "Album2" => $Album2 
)); 
2
$data = array_merge($data, Array("picUpload" => $pPicUpload, "Album1" => $Album1, "Album2" => $Album2)); 
1

爲什麼不乾脆:

$data["picUpload"] = $pPicUpload; 
$data["Album1"] = $Album1; 
$data["Album2"] = $Album2; 
+0

,因爲我知道這是可以做到這一點,因爲我描述,我想了解爲什麼它不適合我 – 2012-08-02 07:19:14

+0

學習新東西總是很好:) – 2012-08-02 07:26:54

0

試試這個:

$data = array("picUpload" => $pPicUpload, "Album1" => $Album1, "Album2" => $Album2); 
0

試試這個,

$data = array("picUpload" => $pPicUpload, "Album1" => $Album1, "Album2" => $Album2); 
+1

這不是什麼保羅問:) – 2012-08-02 07:13:33