I have two arrays and I want to have one array in result and I want also have all values of the arrays even if they has the same values.
I have tried array_combine()
, but with the duplicate keys it is not possible.
I've also tried $array1 + $array2
, but that isn't the desired result either.
These are my sample input arrays:
$array1 = array(10) {
[0]=> string(1) "1"
[1]=> string(0) ""
[2]=> string(2) "12"
[3]=> string(2) "41"
[4]=> string(1) "5"
[5]=> string(1) "6"
[6]=> string(0) ""
[7]=> string(2) "11"
[8]=> string(2) "23"
[9]=> string(2) "10" }
$array2 = array(11) {
[0]=> string(1) "A"
[1]=> string(1) "B"
[2]=> string(1) "C"
[3]=> string(1) "D"
[4]=> string(1) "E"
[5]=> string(1) "F"
[6]=> string(1) "G"
[7]=> string(1) "H"
[8]=> string(1) "I"
[9]=> string(2) "J"
[10]=> string(1) "K" }
I should have output data like this:
$array = array(11){
[1]=> string(1) "A"
[]=> string(1) "B"
[12]=> string(1) "C"
[41]=> string(1) "D"
[5]=> string(1) "E"
[6]=> string(1) "F"
[]=> string(1) "G"
[11]=> string(1) "H"
[23]=> string(1) "I"
[10]=> string(2) "J"
[]=> string(1) "K" }
You can use array_merge like below:
print_r(array_merge($array1, $array2));
or you can use array_combine: array_combine(keys,values);
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
Alert: But you can't duplicate the keys.
You cannot use array_combine
because array_combine
expects the arrays to be the same size. And in fact, the output you ask for is not possible. Array keys need to be unique. You cannot have
[]=> string(1) "B"
[]=> string(1) "G"
[]=> string(1) "K"
as these would overwrite each other. You'd end up with only K.
An alternative would be to assign an array when there are multiple values per key, e.g.
[]=> array(3) ["B", "G", "K"]
There is no built-in function for this though. You'll have to manually iterate both arrays and assign the values as needed. A simple solution would be to use a MultipleIterator
because it can easily deal with differently sized arrays:
$keys = new ArrayIterator(["", 1, "", 2, "", 3]);
$values = new ArrayIterator(["A", "B", "C", "D", "E", "F", "G"]);
$flags = MultipleIterator::MIT_NEED_ANY|MultipleIterator::MIT_KEYS_ASSOC;
$pairs = new MultipleIterator($flags);
$pairs->attachIterator($keys, 'key');
$pairs->attachIterator($values, 'value');
$combined = [];
foreach ($pairs as $pair) {
$key = $pair['key'];
$val = $pair['value'];
if (!isset($combined[$key])) {
$combined[$key] = $val;
continue;
}
if (!is_array($combined[$key])) {
$combined[$key] = [$combined[$key]];
}
$combined[$key][] = $val;
}
print_r($combined);
This would then produce:
Array
(
[] => Array
(
[0] => A
[1] => C
[2] => E
[3] => G
)
[1] => B
[2] => D
[3] => F
)
If you want to combine the two arrays then you can use $array2
's values as keys because they are unique (and it is the longer array so, $array1
won't get cut off).
$array1=["1","","12","41","5","6","","11","23","10"];
$array2=["A","B","C","D","E","F","G","H","I","J","K"];
foreach($array2 as $i=>$v){
$result[$v]=(isset($array1[$i])?$array1[$i]:NULL);
}
var_export($result);
Output:
array (
'A' => '1',
'B' => '',
'C' => '12',
'D' => '41',
'E' => '5',
'F' => '6',
'G' => '',
'H' => '11',
'I' => '23',
'J' => '10',
'K' => NULL,
)
Alternatively, you could pad $array1
then combine:
$array1=["1","","12","41","5","6","","11","23","10"];
$array2=["A","B","C","D","E","F","G","H","I","J","K"];
$array1=array_pad($array1,sizeof($array2),NULL);
$result=array_combine($array2,$array1);