php PHP关联数组重复键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2879132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Associative Array Duplicate Keys
提问by Steven
I have an associative array, however when I add values to it using the below function it seems to overwrite the same keys. Is there a way to have multiple of the same keys with different values? Or is there another form of array that has the same format?
我有一个关联数组,但是当我使用下面的函数向它添加值时,它似乎覆盖了相同的键。有没有办法让多个相同的键具有不同的值?或者是否有另一种形式的数组具有相同的格式?
I want to have:
我希望有:
42=>56
42=>86
42=>97
51=>64
51=>52
etc etc
Code:
代码:
function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key => $value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}
回答by Amber
No, you cannot have multiple of the same key in an associative array.
不,关联数组中不能有多个相同的键。
You could, however, have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.
但是,您可以拥有唯一键,每个键的对应值都是数组,并且这些数组的每个键都有多个元素。
So instead of this...
所以,而不是这个......
42=>56 42=>86 42=>97 51=>64 51=>52
...you have this:
...你有这个:
Array (
42 => Array ( 56, 86, 97 )
51 => Array ( 64, 52 )
)
回答by Mark Tomlin
A key is an extension of a variable. If you overwrite the variable ... you overwrite the variable.
键是变量的扩展。如果你覆盖了变量……你覆盖了变量。
回答by Aris
No, you cannot have. A workaround I use is to have each key/value pair as a new array with 2 elements:
不,你不能有。我使用的解决方法是将每个键/值对作为具有 2 个元素的新数组:
$test = array(
array(42,56),
array(42,86),
array(42,97),
array(51,64),
array(51,52)
)
For example, you can access the second key (=42) using:
例如,您可以使用以下方法访问第二个键 (=42):
$test[1][0]
and the second value(=86) using:
和第二个值(= 86)使用:
$test[1][1]
回答by Michael
I found this question while researching the exact opposite intended outcome, I have an array of data that has duplicate keys! Here's how I did it (still trying to figure out where in my process things are messing up).
我在研究完全相反的预期结果时发现了这个问题,我有一组具有重复键的数据!这是我是如何做到的(仍然试图找出我的过程中哪里出错了)。
$session = time();
$a = array();
$a[(string)$session] = 0;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
)
*/
var_dump($a);
/* output:
array(1)
(
[1510768034] => int(0)
)
*/
print_r($j);
/* output:
{"1510768034":0}
*/
$a = (array)json_decode($j);
$session = @array_pop(array_keys($a));
$a[(string)$session] = 10;
$j = json_encode($a,JSON_FORCE_OBJECT);
print_r($a);
/* output:
Array
(
[1510768034] => 0
[1510768034] => 10
)
*/
var_dump($a);
/* output:
array(2)
(
'1510768034' => int(0)
[1510768034] => int(10)
)
*/
print_r($j);
/* output:
{"1510768034":0,"1510768034":10}
*/
Yup....that just happened.
是的……刚刚发生的。
PHP 7.1
PHP 7.1
Edit: It's similar in PHP 7.2.10, except json_encode no longer entertains duplicate keys, encoded strings are correct. The array, however, can have matching string and integer keys.
编辑:它在 PHP 7.2.10 中类似,除了 json_encode 不再处理重复键,编码的字符串是正确的。但是,该数组可以具有匹配的字符串和整数键。
回答by santosh devnath
it is not possible technically. But I created a fun way to do it . Be alert, this answer is just for the fun, the ultimate goal is to get the output anyway.
这在技术上是不可能的。但我创造了一种有趣的方式来做到这一点。小心,这个答案只是为了好玩,最终目标是无论如何都要得到输出。
So here is the answer. You first need to convert to the whole array to the string. Then the rest of the things is just string replace and a little code.
所以这就是答案。您首先需要将整个数组转换为字符串。然后剩下的就是字符串替换和一些代码。
<?php
$ary="array('a'=>123,'a'=>161,'a'=>195)";
$str = str_replace("array(", "", $ary);
$str = str_replace(")", "", $str);
$arr = explode(",",$str);
foreach($arr as $element){
$element = str_replace("=>", "", $element);
$element = str_replace("'", "", $element);
echo $element.'<br>';
}
?>
回答by Aymen
i had the same need too create an array with the same keys, (just to keep performance by using two loops rather than 4 loops).
我也有同样的需求,创建一个具有相同键的数组,(只是为了通过使用两个循环而不是 4 个循环来保持性能)。
by using this : [$increment."-".$domain_id] => $article_id; my list of articles in each domain looks like this after a print_r() :
通过使用这个: [$increment."-".$domain_id] => $article_id; 在 print_r() 之后,我在每个域中的文章列表如下所示:
$AllSa = Array
(
[1-5] => 143
[2-5] => 176
[3-5] => 992
[4-2] => 60
[5-2] => 41
[6-2] => 1002
[4-45] => 5
[5-45] => 18
[6-45] => 20
)
And then by looping through this table to associate article by domain :
然后通过循环遍历此表以按域关联文章:
$AssocSAPerDomain = array();
$TempDomain = "";
$TempDomain_first = 0;
foreach($tree_array as $id_domain => $id_sa){
if( !$TempDomain && $TempDomain_first == 0 ){ $TempDomain = substr(strrchr($id_domain, "-"), 1); $TempDomain_first = 1; }
$currentDomain = substr(strrchr($id_domain, "-"), 1);
//if($TempDomain == $currentDomain)
$AssocSAPerDomain[$currentDomain][] = $id_sa;
$TempDomain = substr(strrchr($id_domain, "-"), 1);
}
you get this
你得到这个
$assoc= Array
(
[5] => 143
=> 176
=> 992
[2] => 60
=> 41
=> 1002
[45]=> 5
=> 18
=> 20
)


