dousi2029 2011-11-28 20:13
浏览 26
已采纳

从Drupal 7中的对象属性创建一个数组,php

I'm creating this form with checkboxes that I want to populate from a database. The problem is, how do I create an array of key=>value pairs when the data I need to create the pairs is in an object? I'm not sure I'm explaining myself properly, heres' the code and hopefully it will be clearer:

function myform_form($form, &$form_state) {
  $options_query = db_query('SELECT name, mname FROM event_type');
  $options = array();
  foreach($options_query as $o) {
    $options(($o->mname) => ($o->name));  //This is where I get the error unexpected T_DOUBLE_ARROW
  }
  $form['options'] = array(
    '#type' => 'checkboxes', 
    '#title' => t('Search options'),
    '#options' => $options,
    '#description' => t('Choose what you want.'),
 );

Is there a way to do this?

  • 写回答

1条回答 默认 最新

  • dongwenghe2416 2011-11-28 20:16
    关注

    Try changing:

      $options(($o->mname) => ($o->name)); 
    

    to

     $options[$o->mname] = $o->name; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?