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

发布于 2024-12-10 21:40:52 字数 718 浏览 0 评论 0原文

我想根据多维数组内的数字动态创建一个数组,

这里是代码

$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,则其中必须有 2 个具有相同名称的数组,desc ,type,str 但 ID 不同。

这可能是某种方式吗?

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.

is this possible is somekind of way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2024-12-17 21:40:52

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

演示

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

$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

岁月流歌 2024-12-17 21:40:52

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

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

您将在浏览器中得到如下输出:

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
                )
        )
)

演示

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

歌入人心 2024-12-17 21:40:52

首先,您创建数组 $meta_box,如下所示:

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

这将从 1 开始对 ids 进行编号,直到 $number。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文