php PHP在多维数组中创建动态数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7821878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:26:21  来源:igfitidea点击:

PHP create dynamic array in a multidimensional array

phparraysmultidimensional-array

提问by Peter Otten

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',
'title' => 'Custom Input Fields',
'page' => 'page',
'context' => 'normal',
'priority' => 'high',
'fields' => array (
                  array( //this array must be created dynamic 
                      'name' => 'Textarea',
                      'desc' => 'Enter big text here',
                      'id' => 'textarea', //id is textarea + number
                      'type' => 'textarea',
                      'std' => 'Default value'
                  )
            )
);

I want the last array to be created dynamic by a number so if the number is 2 there must be 2 arrays in there with the same name,desc,type,str but a diffrent ID.

我希望最后一个数组由一个数字动态创建,因此如果数字为 2,则其中必须有 2 个具有相同名称、desc、type、str 但 ID 不同的数组。

is this possible is somekind of way?

这可能是某种方式吗?

回答by hakre

Just add them dynamically by iterating over the number of ids:

只需通过迭代 id 的数量来动态添加它们:

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


$dynamicNumber = 2;
$idPrefix = 'textarea';
assert('$dynamicNumber > 0');
$dynamicIds = range(1, $dynamicNumber);

$fields = &$meta_box['fields'];
foreach($dynamicIds as $id)
{
    $fields[] = array( //this array must be created dynamic 
                      'name' => 'Textarea',
                      'desc' => 'Enter big text here',
                      'id' => sprintf('%s%d', $idPrefix, $id), //id is textarea + number
                      'type' => 'textarea',
                      'std' => 'Default value'
                  );
}
unset($fields);

Demo

演示

回答by Bert

Here's a way to add each 'fields' sub array as a new array into the larger array

这是将每个“字段”子数组作为新数组添加到更大数组中的方法

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

$fields = array();

$numberOfArrays = 2;

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

$meta_box['fields'] = $fields;

echo '<pre>';
print_r($meta_box);
echo '</pre>';

You'll get an output like this in your browser:

你会在浏览器中得到这样的输出:

Array
(
    [id] => my-meta-box
    [title] => Custom Input Fields
    [page] => page
    [context] => normal
    [priority] => high
    [fields] => Array
        (
            [1] => Array
                (
                            [name] => Textarea
                            [desc] => Enter big text here
                            [id] => textarea1
                            [type] => textarea
                            [std] => Default value
                )
        [2] => Array
                (
                            [name] => Textarea
                            [desc] => Enter big text here
                            [id] => textarea2
                            [type] => textarea
                            [std] => Default value
                )
        )
)

Demo

演示

回答by Jan-Henk

First you create the array $meta_box as follows:

首先创建数组 $meta_box 如下:

$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.

这将从 1 开始编号,直到 $number。