2011-11-02 115 views
-2

我創建需要持有一些像這樣的空值的數組:如何將一個空值插入到PHP數組中?

$my_array['message'] = 'hello there'; 
$my_array['sender'] = 'Billy Bob'; 
$my_array['comment'] = ''; 

var_dump($my_array); 

VAR轉儲的結果是:

array 
    'message' => 'hello there'; 
    'sender' => 'Billy Bob'; 

我想要的結果看起來是這樣的:

array 
    'message' => 'hello there'; 
    'sender' => 'Billy Bob'; 
    'comment' => ''; 

這可能嗎?

+2

你試過了嗎? – Marcus

+1

事實上,[確切發生了什麼](http://ideone.com/429nh)。 – Jon

+0

@Marcus正如你所看到的,他發佈了'var_dump'結果。 – hsz

回答

-1

瓦爾轉儲顯然將跳過空值。請用isset或==''

+0

var_dump將包含所有存在的元素,即使它們包含一個空值。 – GordonM

+0

我的不好,感謝糾正我!我只是假設基於OP – miahelf

0
$array ['key'] = ''; // Sets an element in the array called 'key' to an empty string