PHP create dynamic array in a multidimensional array

前端 未结 3 868
清酒与你
清酒与你 2021-01-29 09:36

I want to dynamic create an array based on a number inside a multidimensional array

here is the code

$meta_box = array(  
\'id\' => \'my-meta-box\',
         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 09:44

    First you create the array $meta_box as follows:

    $meta_box = array(  
      'id' => 'my-meta-box',
      'title' => 'Custom Input Fields',
      'page' => 'page',
      'context' => 'normal',
      'priority' => 'high',
      'fields' => array ()
    );
    

    Then you can add the 'dynamic' arrays as follows:

    $number = 2;
    for ($i = 1; $i <= $number; $i++) {
      $meta_box['fields'][] = array(
        'name' => 'Textarea',
        'desc' => 'Enter big text here',
        'id' => 'textarea_' . $i, //id is textarea + number
        'type' => 'textarea',
        'std' => 'Default value'
      );
    }
    

    This starts the numbering for the ids at 1 until $number.

提交回复
热议问题