2013-12-17 42 views
1

我有一個這樣的數組。Symfony2.4.0 formbuilder 2d數組值

array(
1 => array("United States","Antigua and Barbuda","Anguilla","American Samoa","Barbados","Bermuda","Bahamas","Canada","Dominica","Dominican Republic","Grenada","Guam","Jamaica","Saint Kitts and Nevis","Cayman Islands","Saint Lucia","Northern Mariana Islands","Montserrat","Puerto Rico","Sint Maarten (Dutch part)","Turks and Caicos Islands","Trinidad and Tobago","Saint Vincent and the Grenadines","Virgin Islands, U.S.","Virgin Islands, U.S.") 
,7 => array("Russian Federation","Kazakhstan") 
,20 => array("Egypt") 
,27 => array("South Africa") 
,30 => array("Greece") 
,31 => array("Netherlands") 
,32 => array("Belgium") 
,33 => array("France") 
,34 => array("Spain") 
,36 => array("Hungary") 
,39 => array("Italy") 
,40 => array("China") 
,41 => array("Switzerland") 
,43 => array("Austria") 
,44 => array("United Kingdom","Guernsey","Isle of Man","Jersey") 
,45 => array("Denmark") 
,46 => array("Sweden") 
,47 => array("Norway","Svalbard and Jan Mayen") 
,48 => array("Poland") 
) 

我希望它的HTML選擇元素中的值有一個與父鍵相同的值。現在我正在使用

$builder->add('callingCode', 'choice', array(
     'choices' => $thatarray)); 

但這樣會導致optgroups這樣。

1 
    United States 
    Antigua and Barbuda 
    Anguilla 
    ... 
7 
    Russian Federation 
    Kazakhstan 
20 
    Egypt 
... 

美國爲0,安提瓜值和巴布達的值爲1,安圭拉具有爲2的值,俄羅斯聯邦爲0,值等,這不是我想要的。美國,安提瓜和巴布達以及安圭拉的價值應該是1.俄羅斯聯邦和哈薩克斯坦的價值應該是7,而埃及的價值應該是20.這樣的事情是可能的嗎?

回答

0

你需要做一個數組的鍵值

$thatarray=array(
"1" => "United States" 
"1" =>"Antigua and Barbuda", 
"1" =>"Anguilla", 
"1" =>"American Samoa", 
"1" =>"Barbados", 
"7" => "Russian Federation", 
"7" =>"Kazakhstan", 
"20" => "Egypt", 
./*other countries */ 
. 
. 
. 
. 
); 

$builder->add('callingCode', 'choice', array('choices' => $thatarray)); 
+1

因爲有一個鍵多個值這是行不通的。在這種情況下,只會顯示巴巴多斯,哈薩克斯坦和埃及。 –