1

我有一个具有多个选项的值的数组,该值被分解为它自己的数组。我想用循环填充其中一个值。在forloop中这是否可能,;我认为它会中断。完成此请求的最佳方法是什么?

  array(
    'name' => __('Ensemble List', 'januas'),
    'type' => 'checkbox',
    'options' => array(
     for ($x=0; $x<=10; $x++){
     array('name' => __($x , 'title'), 'value' => $x)
      }
    )
  ),

这是我发布的内容之外的完整数组:

$meta_boxes[] = array(
    'id' => 'januas_speakers',
    'title' => __('Ensemble', 'januas'),
    'pages' => array('ja-event'),
    'context' => 'normal',
    'priority' => 'high',
    'show_names' => true,
    'fields' => array(
      array(
        'name' => __('Visible', 'januas'),
        'desc' => __('Select Yes to show the box in the event page, No to hide it.', 'januas'),
        'id' => 'januas_speakers_visible',
        'type' => 'select',
        'options' => array(
          array('name' => __('Yes', 'januas'), 'value' => 'y'),
          array('name' => __('No', 'januas'), 'value' => 'n'),
        )
      ),
      array(
        'name' => __('Position', 'januas'),
        'desc' => __('Select the preferred position for the box.', 'januas'),
        'id' => 'januas_speakers_position',
        'type' => 'select',
        'options' => array(
          array('name' => __('Main', 'januas'), 'value' => 'main'),
          array('name' => __('Sidebar', 'januas'), 'value' => 'sidebar'),
        )
      ),
      array(
        'name' => __('Order', 'januas'),
        'desc' => __('Insert the box order (ex: 1).', 'januas'),
        'id' => 'januas_speakers_order',
        'std' => 1,
        'type' => 'text_small'
      ),
      array(
        'name' => __('Show Title', 'januas'),
        'desc' => __('Select Yes to show the box title, No to hide it.', 'januas'),
        'id' => 'januas_speakers_showtitle',
        'type' => 'select',
        'options' => array(
          array('name' => __('Yes', 'januas'), 'value' => 'y'),
          array('name' => __('No', 'januas'), 'value' => 'n'),
        )
      ),
      array(
        'name' => __('Show in Top menu', 'januas'),
        'desc' => __('Select Yes to show the menu item in the event page top menu, No to hide it.', 'januas'),
        'id' => 'januas_speakers_showinmenu',
        'type' => 'select',
        'options' => array(
          array('name' => __('Yes', 'januas'), 'value' => 'y'),
          array('name' => __('No', 'januas'), 'value' => 'n'),
        )
      ),
       array(
        'name' => '',
        'desc' => '',
        'id' => 'januas_images_gallery',
        'type' => 'image_gallery'
      ),
      array(
        'name' => __('Ensemble List', 'januas'),
        'desc' => 'Select the ensemble memebers for this event.',
        'id' => 'januas_speakers_completelist',
        'type' => 'checkbox',
        'options' => array_map(function ($x) {
            return array(
        'name' => __($x, 'januas'),
        'value' => $x,
      );
    }, range(0,11))
  ),
      array(
        'name' => __('Display order', 'januas'),
        'desc' => '',
        'id' => 'januas_speakers_speakersorder',
        'type' => 'event_speakers'
      ),
      array(
        'name' => '',
        'desc' => '',
        'id' => 'januas_speakers_backtotop',
        'type' => 'backtotop'
      )
    ),
  );
4

3 回答 3

3

你可以使用array_map

array(
  'name' => __('Ensemble List', 'januas'),
  'type' => 'checkbox',
  'options' => array_map(function ($x) {
    return array(
      'name' => __($x, 'title'),
      'value' => $x,
    );
  }, range(0,11))
),
于 2013-10-17T01:46:11.630 回答
0

你应该使用array_push

$a = array(
    'name' => __('Ensemble List', 'januas'),
    'type' => 'checkbox',
    'options' => array()
  );
  for ($x=0; $x<=10; $x++){
     array_push( $a['options'], array('name' => __($x , 'title'), 'value' => $x) );
  }
 print_r($a);
于 2013-10-17T01:49:55.207 回答
0

您是否需要在选项内运行循环?这种方法怎么样。

$foo = array();

for ($x=0;$x<10;$x++)
    $foo[] = $x;

$bar = array(
    'name' => 'hello',
    'options' => $foo
);
print_r($bar);

https://eval.in/54925

于 2013-10-17T01:55:03.487 回答