如何保持相同的数组键PHP

问题描述 投票:0回答:3

任何人都可以让我知道如何在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>';
php arrays
3个回答
1
投票

这不能以你想要的方式完成。数组键是标识数组中条目的内容。您只能拥有一个具有相同键的元素。因此,在您的示例中,使用分隔符键的第二个元素将覆盖第一个元素。

所以你必须以其他方式解决它。一种方法是不使用显式键,只使用数字索引。您已将键值作为每个数组条目中的id字段。

这看起来像这样:

$data = [
    array( 
        'type' => 'checkbox',
        'id' => 'username',
        'label' => 'Show Username Field',
        'default' => true,
    ),
    array( 
        'type' => 'separator',
        'height' => 'thin'
    ),       
    array( 
        'type' => 'textarea',
        'id' => 'heading',
        'label' => 'Heading Text',
        'default' => '',
    ),
    array( 
        'type' => 'separator',
        'height' => 'thin'
    ),
    array( 
        'type' => 'text',
        'id' => 'placeholder',
        'label' => 'Placeholder Text',
        'default' => 'Your name',
    ),
];
echo '<pre>';
print_r($data);
echo '</pre>';

您必须遍历条目才能找到特定元素。但是如果你从数据生成html,我想你无论如何都在循环它。


1
投票

在关联数组中不可能有相同的键。

如果给出相同的键,它将被覆盖。

根据您的数据,我假设您正在尝试呈现表单。

基于此,以下是我的建议。

  1. 在数组中移动键名并使用它。

$data = [
    [
        'key_name' => 'username',
        'type' => 'checkbox',
        'id' => 'username',
        'label' => 'Show Username Field',
        'default' => true,
    ],
    [
        'key_name' => 'separator',
        'type' => 'separator',
        'height' => 'thin'
    ],
    [
        'key_name' => 'heading',
        'type' => 'textarea',
        'id' => 'heading',
        'label' => 'Heading Text',
        'default' => '',
    ],
    [
        'key_name' => 'separator',
        'type' => 'separator',
        'height' => 'thin'
    ],
    [
        'key_name' => 'placeholder',
        'type' => 'text',
        'id' => 'placeholder',
        'label' => 'Placeholder Text',
        'default' => 'Your name',
    ],
];
  1. 或者,如果您仍然希望键名相同,请将其作为数组数组。

$data = [
    [
        'username' => [ 
            'type' => 'checkbox',
            'id' => 'username',
            'label' => 'Show Username Field',
            'default' => true,
        ],
    ],
    [
        'separator' => [ 
            'type' => 'separator',
            'height' => 'thin'
        ],
    ],
    [
        'heading' => [ 
            'type' => 'textarea',
            'id' => 'heading',
            'label' => 'Heading Text',
            'default' => '',
        ],
    ],
    [
        'separator' => [ 
            'type' => 'separator',
            'height' => 'thin'
        ],
    ],
    [
        'placeholder' => [ 
            'type' => 'text',
            'id' => 'placeholder',
            'label' => 'Placeholder Text',
            'default' => 'Your name',
        ],
    ]
];

0
投票

数组格式错误,初始化数组如下所示。

$data = [
'username' => array(
        'type' => 'checkbox',
        'id' => 'username',
        'label' => 'Show Username Field',
        'default' => true,
    ),
'separator' => array(
               array(
                        'type' => 'separator',
                        'height' => 'thin'
                    ),
                array(
                   'type' => 'separator',
                   'height' => 'thin'
                 )
            ),
'placeholder' => array(
        'type' => 'text',
        'id' => 'placeholder',
        'label' => 'Placeholder Text',
        'default' => 'Your name',
    ),
];
echo '<pre>';
print_r($data);
echo '</pre>
© www.soinside.com 2019 - 2024. All rights reserved.