如何允许php数组中的重复键 [英] How to allow duplicate keys in php array

查看:35
本文介绍了如何允许php数组中的重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让php数组有重复的键?当我尝试插入一个键值对时,它会用新值覆盖对应的先前键的值.有没有办法可以同时维护两个具有不同值的重复键?

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?

推荐答案

您可以拥有一个具有数组值(也称为多维数组)的键,该数组将包含具有该给定键的所有元素.一个例子可能是

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")
);

这篇关于如何允许php数组中的重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆