How to Allow Duplicate Keys in PHP Array

PHP Associative Array Duplicate Keys

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

Showing duplicate keys in associative array in PHP

You can store results as array of arrays as approach:

<?php
$arrNum = [2, 5, 6, 6, 8];

$arrNumDouble = array_map(
function($el) {
return [$el, 2*$el];
},
$arrNum
);

#var_export($arrNumDouble);

foreach($arrNumDouble as $el) {
echo "2 * $el[0] = $el[1]" . PHP_EOL;
}

share PHP code

Keep duplicate keys when fill keys for multilevel arrays

1st solution : You cannot get what you want as same indexes in php array got over-write:https://3v4l.org/r934K

What best possible you can get is:

$result = [];

foreach($arr1 as $key=>$value){
$result[array_unique($value)[0]] = $arr2[$key];
}

Output: https://3v4l.org/DfPU3

2nd soultion : For the output what you want, you need to apply one more foreach()

$result = [];

foreach($arr1 as $key=>$value){
foreach($value as $k=>$val){
$result[$key][$k] = [$val,$arr2[$key][$k]];
}
}

print_r($result);

Output : https://3v4l.org/u8JmX

Get the keys for duplicate values in an array

function get_keys_for_duplicate_values($my_arr, $clean = false) {
if ($clean) {
return array_unique($my_arr);
}

$dups = $new_arr = array();
foreach ($my_arr as $key => $val) {
if (!isset($new_arr[$val])) {
$new_arr[$val] = $key;
} else {
if (isset($dups[$val])) {
$dups[$val][] = $key;
} else {
$dups[$val] = array($key);
// Comment out the previous line, and uncomment the following line to
// include the initial key in the dups array.
// $dups[$val] = array($new_arr[$val], $key);
}
}
}
return $dups;
}

obviously the function name is a bit long;)

Now $dups will contain a multidimensional array keyed by the duplicate value, containing each key that was a duplicate, and if you send "true" as your second argument it will return the original array without the duplicate values.

Alternately you could pass the original array as a reference and it would adjust it accordingly while returning your duplicate array

php split a file into $key=$value pairs with duplicate keys

I think your problem is you override the value for your key.

$returnArray[$key] = $value;

so what yo want is to append your values to a subarray with the $key as the parent.

$returnArray[$key][] = $value;

With the [] you append the $value to the underlying array.

How do I convert a string to an associative array when the string contains a duplicate key in PHP?

Check if there's already an element in the array with the key before adding it.

    for($i=0; $i<$n; $i++)
{
if (!isset($params[$matches[1][$i]])) {
$params[$matches[1][$i]] = $matches[2][$i];
}
}
var_dump($params);


Related Topics



Leave a reply



Submit