【问题标题】:How to keep an identical array key PHP如何保持相同的数组键PHP
【发布时间】: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

标签: php arrays


【解决方案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,我猜你还是在循环遍历它。

【讨论】:

    【解决方案2】:

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

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

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

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

    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',
            ],
        ]
    ];
    

    【讨论】:

    • 这可能有效。在我的示例中,key_name 与 id 名称相同,因此我使用了 Jacob Oettinger 的示例。谢谢!
    • 但是分隔符中没有id
    • 我只为包含值的字段创建了一个id,分隔符仅用于外观。所以分隔符 id 将在函数中预先定义。
    【解决方案3】:

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

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

    【讨论】:

    • 我想用这个构造一个html表单,所以键的顺序很重要。我想用“分隔符”分隔每个字段。我不知道这是否有效。我必须测试它。谢谢!
    • 是的,你可以。根据您的要求更改上述代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2022-01-19
    • 2020-08-10
    相关资源
    最近更新 更多