PHP Associative Array Duplicate Keys

2019-01-01 09:13发布

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;
    }

标签: php
5条回答
冷夜・残月
2楼-- · 2019-01-01 09:46

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

查看更多
低头抚发
3楼-- · 2019-01-01 09:47

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 )
)
查看更多
听够珍惜
4楼-- · 2019-01-01 10:01

A key is an extension of a variable. If you overwrite the variable ... you overwrite the variable.

查看更多
大哥的爱人
5楼-- · 2019-01-01 10:09

No, you cannot have. A workaround I use is to have each key/value pair as a new array with 2 elements:

$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:

$test[1][0]

and the second value(=86) using:

 $test[1][1] 
查看更多
君临天下
6楼-- · 2019-01-01 10:09

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).

by using this : [$increment."-".$domain_id] => $article_id; my list of articles in each domain looks like this after a 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
)
查看更多
登录 后发表回答