How to allow duplicate keys in php array?

Web Development

How to allow php array to have duplicate keys?

When I try to insert a key, value pair with already existing key it overwrites the value of corresponding previous key with the new value. Is there a way that I could maintain both duplicate keys having different values?



Ans: It is possible to have a single key that has a value of an array, which consists of all the elements with that given key. Below is the example that could help you understand better.

$countries = array(

  "United States" => array("Texas", "California"),

  "Canada" => array("Quebec", "Ontario")



If you want to unleash your potential in this competitive field, please visit the Web Development course page for more information, where you can find the Web Development tutorials and Web Development frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.