How to allow duplicate keys in php array

0 votes
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?
Nov 19, 2020 in PHP by kartik
• 37,520 points
3,937 views

1 answer to this question.

0 votes

Hello @kartik,

You could have a single key that has a value of an array(aka a multi-dimensional array), which would contain all the elements with that given key. An example might be

$countries = array(
  "United States" => array("California", "Texas"),
  "Canada" => array("Ontario", "Quebec")
);

Hope it helps!!

answered Nov 19, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
2,056 views
0 votes
1 answer

How to access array returned by a function in php?

Hello @kartik, Since PHP 5.4 it's possible to ...READ MORE

answered Nov 10, 2020 in PHP by anonymous
• 82,840 points
861 views
0 votes
1 answer

How to pass an array via $_GET in php?

Hii, You can pass an associative array to http_build_query() and ...READ MORE

answered Nov 22, 2020 in PHP by Niroj
• 82,840 points
4,457 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
622 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
2,737 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
3,016 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP