循环选项到数组

时间:2013-10-17 01:42:19

标签: php arrays for-loop

我有一个数组,其中包含多个选项的值,这些选项可以分解为自己的数组。我希望用循环填充其中一个值。这可能与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'
      )
    ),
  );

3 个答案:

答案 0 :(得分: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))
),

答案 1 :(得分: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);

答案 2 :(得分:0)

你需要在选项中运行循环吗?这种方法怎么样。

$foo = array();

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

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

https://eval.in/54925