如何让在PHP数组重复键 [英] how to allow duplicate keys in php array

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

问题描述

任何人都可以让我知道如何让PHP数组有重复键。当我尝试插入一个键,值对已经存在的键,它会覆盖相应的新价值previous键的值。有,我可以保持具有不同价值观都重复键的方式?

Could anyone let me know 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天全站免登陆