0

Could anyone let me know how can I allow duplicate keys in php array? I have already read all identical posts concerning this questions and it does not answer my question.

In the following example, I want to pass the second 'separator' key without rename it.

$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>'; 
5
  • 2
    And when you request $data['separator'] - which value should be returned? Commented Dec 16, 2017 at 8:17
  • 2
    And if you have type in subarray - why do you need a key then? Use zero-based indexed array. Commented Dec 16, 2017 at 8:18
  • this $data['separator'] reternig : Array Commented Dec 16, 2017 at 8:22
  • and the print_r($data); it returning only the first 'separator' Commented Dec 16, 2017 at 8:24
  • Read about PHP arrays. Commented Dec 16, 2017 at 8:39

3 Answers 3

1

That cannot be done in the way you are trying to do it. The array key is what identifies the entry in the array. You can only have one element with the same key. So in your example your second element with the separator key is overwriting the first element.

So you have to solve it in some other way. One way would be to not use explicit keys and just us numeric indexes. You already have your key value as the id field in each of your array entries.

That would look something like this:

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

You would have to loop through the entries to find a specific element. But if you are generating html from the data, I guess you are looping through it anyway.

Sign up to request clarification or add additional context in comments.

Comments

1

It is not possible to have identical keys in an associative array.

If identical keys are given, it gets overridden.

From your data, I assume you are trying to render a form.

Based on that, below are my suggestions.

  1. Move the key name inside the array and utilize it.

Example

$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. Or if you still want the key name in the same order, have it as array of arrays.

Example

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

3 Comments

this probably works. in my example the key_name is same as id name, so i had used the example from Jacob Oettinger. Thank you!
but there is no id in separator ?
I maked an id only for fields that contain an value, the separator is only for appearance. so the separator id it will be predifined in the function.
0

Array is in wrong format initilize array as given below.

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

2 Comments

I want to construct a html form with this, so the keys order is important. I want to separate every field with a 'separator'. I dont know if this work. I must to test it. thanks!
yes you can . change above code according to your requierment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.