Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在多维数组中创建动态数组_Php_Arrays_Multidimensional Array - Fatal编程技术网

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

PHP在多维数组中创建动态数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想基于多维数组中的数字动态创建一个数组 这是密码 $meta_box = array( 'id' => 'my-meta-box', 'title' => 'Custom Input Fields', 'page' => 'page', 'context' => 'normal', 'priority' => 'high', 'fields' => array ( array( //this array must be

我想基于多维数组中的数字动态创建一个数组

这是密码

$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'
                  )
            )
);
我希望最后一个数组是由一个数字动态创建的,所以如果这个数字是2,那么其中必须有两个数组,它们的名称、desc、type、str相同,但ID不同


这可能是某种方式吗?

只需通过迭代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);

首先创建数组$meta_框,如下所示:

$meta_box = array(  
  'id' => 'my-meta-box',
  'title' => 'Custom Input Fields',
  'page' => 'page',
  'context' => 'normal',
  'priority' => 'high',
  'fields' => array ()
);
$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'
  );
}
然后可以按如下方式添加“动态”数组:

$meta_box = array(  
  'id' => 'my-meta-box',
  'title' => 'Custom Input Fields',
  'page' => 'page',
  'context' => 'normal',
  'priority' => 'high',
  'fields' => array ()
);
$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'
  );
}

ID的编号从1开始,直到$number。

这里有一种方法可以将每个“字段”子数组作为新数组添加到较大的数组中

$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>';

你有没有尝试过用循环创建数组?你能给我一个例子,说明我是如何真正地使用你的一些代码,并告诉我们你希望它做什么吗。