【发布时间】:2017-12-16 16:35:05
【问题描述】:
谁能告诉我如何在 php 数组中允许重复键?我已经阅读了有关此问题的所有相同帖子,但它没有回答我的问题。
在以下示例中,我想传递第二个“分隔符”键而不重命名它。
$data = [
'username' => array(
'type' => 'checkbox',
'id' => 'username',
'label' => 'Show Username Field',
'default' => true,
),
'separator' => array(
'type' => 'separator',
'height' => 'thin'
),
'heading' => array(
'type' => 'textarea',
'id' => 'heading',
'label' => 'Heading Text',
'default' => '',
),
'separator' => array(
'type' => 'separator',
'height' => 'thin'
),
'placeholder' => array(
'type' => 'text',
'id' => 'placeholder',
'label' => 'Placeholder Text',
'default' => 'Your name',
),
];
echo '<pre>';
print_r($data);
echo '</pre>';
【问题讨论】:
-
当你请求
$data['separator']- 应该返回哪个值? -
如果你在子数组中有
type- 那你为什么需要一个密钥呢?使用从零开始的索引数组。 -
这个 $data['separator'] reternig : 数组
-
和 print_r($data);它只返回第一个“分隔符”
-
@MustaphaFersaoui 请阅读how to allow duplicate keys in php array