在Select - FORM API Drupal中显示数组

Kri*_*511 0 php drupal drupal-fapi

我想添加键(type_id)和值(type_description)以在drupal表单API中进行选择

$ result_x-> product_types-> RPMProductType是来自数据库的数组结果: - array(4){[0] => object(stdClass)#18(2){["type_description"] => string(10)"Calendered"[ "type_id"] => int(1)} [1] => object(stdClass)#19(2){["type_description"] => string(8)"Extruded"["type_id"] => int(2 )} [2] => object(stdClass)#20(2){["type_description"] => string(6)"Molded"["type_id"] => int(3)} [3] => object( stdClass)#21(2){["type_description"] => string(5)"Other"["type_id"] => int(4)}}

foreach ($result_x->product_types->RPMProductType as $data)
{

$form['manufacturer_add_new_sales']['product_type'] = array(
    '#type' => 'select',
    '#title' => t('Product Type'),
    '#options'=>array($data->type_id=>$data->type_description),
    );
}

什么时候这样做我只得到最后一个值,即其他.如何正确循环绑定选择以显示所有数组键 - 值.

先感谢您.

goo*_*orp 6

您需要创建一个包含值的数组并使用它.

foreach ($array as $key => $value) {
  $options[$key] = $value;
}
Run Code Online (Sandbox Code Playgroud)

然后您可以使用$ options作为选项.