2014-07-15 51 views
2

使用Zend框架1.9。我有一個表中有許多列的數據庫。我只取某些列:Zend Framerwork 1.9填充數據庫查詢中的複選框

$select = $table->select(); 
$select->from($table, array('skill_id', 'description'))->where('parent_skill IS NULL'); 
$rows = $table->fetchAll($select); 

print_r($rows->toArray()); 

//This is the output : 
Array ([0] => Array ([skill_id] => 1 [description] => Soccorso stradale) [1] => Array ([skill_id] => 4 [description] => Carrozziere)) 

現在我想填充一個複選框元素:

$form->skills->setMultiOptions ($rows->toArray()); 

我的問題是方法setMultiOptions期待一個陣列array(key=>value)。 是否可以將我的$rows數組轉換爲格式良好的數組?

回答

1

着力構建陣列像這樣選擇:

$row_options = array(); 
foreach($rows->toArray() as $key => $val){ 
    $row_options[$val['skill_id']] = $val['description']; 
} 

$form->skills->setMultiOptions ($row_options); 
0

嗯,是的,你可以我想

foreach($rows->toArray() as $subArray){ 
     foreach($subArray as $val){ 
      $newArray[] = $val; 
     } 
    } 
print_r($newArray);die; 
-1

您可以使用fetchPair

.... ...

$ select-> from($ table,array('skill_id','description')) - > where('parent_skill IS NULL');

$ rows = $ table->fetchPair($ select);

$ form-> skills-> setMultiOptions($ row_options);