0

drupal形式のAPIで選択するキー(type_id)と値(type_description)を追加したい

$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) "その他" ["type_id"]=> int(4) } }

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

$form['manufacturer_add_new_sales']['product_type'] = array(
    '#type' => 'select',
    '#title' => t('商品タイプ'),
    '#o​​ptions'=>array($data->type_id=>$data->type_description),
    );
}

そうするとき、最後の値、つまりその他のみを取得しています。正しくループしてバインドする方法 すべての配列キー - 値を表示するように選択します。

前もって感謝します。

4

2 に答える 2

6

値を含む配列を作成し、それを使用する必要があります。

foreach ($array as $key => $value) {
  $options[$key] = $value;
}

次に、オプションとして $options を使用できます。

于 2010-05-03T21:40:11.117 に答える
0

$options 配列の各 $key を設定する代わりに、関数を使用して配列を返すこともできます。

'#options'=> function_options($param),
....
....
....

// Your Options populating function
function_options($param){
$optionarray = array();
// Populate array with DB values
.....
..... 
return optionarray;
}
于 2010-10-22T20:29:12.313 に答える