我想根据多维数组内的数字动态创建一个数组
这是代码
$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 不同的数组。
这可能是某种方式吗?
只需通过迭代 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 http://codepad.org/WhnTyOZa
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)