I want to go from these arrays:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
To this array:
$arrayResult =
array(
array("x" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"y" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"z" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)))
);
I tried to make this combined array with cartesian product approaches, but no satisfying result so far.
Here is another solution without using any loop:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
$result = array_combine(
$array1,
array_fill(
0,
count($array1),
array_combine(
$array2,
array_fill(0, count($array2), $array3)
)
)
);
print_r($result);
Here is the demo
use array_fill_keys twice to get the result
$result = array_fill_keys(
$array1,
array_fill_keys($array2, $array3)
);
Demo on eval.in
Related
I have two arrays:
$array1 = [29, 'a', 'x', 'c', 11];
$array2 = ['a' => 20, 'x' => 21, 'c' => 23];
I want to get an array that looks like:
$array3 = [29, 20, 21, 23, 11];
I know how to do it with a foreach loop, but I was wondering if there was a way to do it as a one liner, or maybe with some sort of anonymous function.
array_map work as well the other answer :
$array1 = [29, 1=>'a', 2=>'x',3=>'c', 11];
$array2 = ['a'=>20, 'x'=>21, 'c'=>23];
$array3 = array_map(function($a) use($array2){return is_int($a) ? $a : $array2[$a];}, $array1);
You can add them and filter out non-numeric values:
$array3 = array_values(array_filter($array1 + $array2, 'is_numeric'));
If the order is important:
$array3 = array_filter(call_user_func_array(
'array_merge', array_map(null, $array1, $array2)),
'is_numeric');
Then array_values if you need to re-index.
In either case, if you want only integers or floats then use is_int or is_float.
An attempt with a one-liner :
$array1 = [29, 1=>'a', 2=>'x',3=>'c', 11];
$array2 = ['a'=>20, 'x'=>21, 'c'=>23];
$array3 = array_values(array_filter(array_merge($array1,$array2),function($i){return is_int($i);}));
print_r($array3);
// Outputs :
/*
Array
(
[0] => 29
[1] => 11
[2] => 20
[3] => 21
[4] => 23
)
*/
I reckon this looks more elegant than the earlier posted solutions...
Code: (Demo)
var_export(
array_map(fn($v) => $array2[$v] ?? $v, $array1)
);
Attempt to access the mapping array's value by key; if there is no matching key, then default to the original value.
If you don't like to use foreach() you can use array_replace() function. You can learn more about this function in below:
Array_replace($array1 , $array2, $...) in php.net
I have two arrays with different length, but keys are similar.
My requirement is update $array1 with values of $array2 similar keys
$array1 = array("Jan"=>"0", "Feb"=>"0", "Mar"=>"0", "Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
Output:
$res = array("Jan"=>"2","Feb"=>"0","Mar"=>"3","Apr"=>"0");
You can achieve it by this code:
$array1 = array("Jan" => "0", "Feb" => "0", "Mar" => "0", "Apr" => "0");
$array2 = array("Jan" => "2", "Mar" => "3");
$array3 = array_replace($array1, $array2);
print_r($array3);
You can simply use + operator.
$array1 = array("Jan"=>"0","Feb"=>"0","Mar"=>"0","Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
print_r($array2 + $array1);
DEMO
Try this:
array_merge($array1, $array2);
I have two associative array.
$Array1 = array(
'abc'=> 'abc',
'def'=> 'def'
);
$Array2 = array(
'123'=> '123',
'456'=> '456'
);
I am merging them using array_merge.
$Array3 = array_merge($Array1, $Array2);
Now value of $Array3 is like this.
Array
(
[abc] => abc
[def] => def
[0] => 123
[1] => 456
)
It looks really odd until I read php manual which says Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. array_merge manual
My questing How can I merge both array without loosing their associative keys.
Both array can have common KEYS and I don't want to loose my information also. :(
By +:
$Array1 = array(
'abc'=> 'abc',
'def'=> 'def'
);
$Array2 = array(
'123'=> '123',
'456'=> '456'
);
var_dump($Array1 + $Array2);
This will preserve the index, but note this will not overwrite the value of the first array if same key exists in first array.
And if you want the overwrite working, then there is array_replace function for this:
var_dump(array_replace($Array1, $Array2));
Try this code :) it will work
function my_merge($array1,$array2)
{
foreach($array2 as $key => $value)
{
$array1[$key] = $value;
}
return $array1;
}
$Array1 = array(
'abc'=> 'abc',
'def'=> 'def'
);
$Array2 = array(
'123'=> '123',
'456'=> '456'
);
$Array3 = my_merge($Array1, $Array2);
For associative arrays, use
$result = $Array1 + $Array2;
-but note, that for numeric keys this will also re-index:
$Array1 = array(
'abc',
'def'=> 'def'
);
$Array2 = array(
'123',
'456'
);
var_dump($Array1 + $Array2);
//array(3) { [0]=> string(3) "abc" ["def"]=> string(3) "def" [1]=> string(3) "456" }
If you have same keys in your arrays, you can use:
$result = array_reduce(array_keys($Array1), function($c, $x) use ($Array1)
{
$c[$x] = isset($c[$x])
?array_merge((array)$c[$x], [$Array1[$x]])
:$Array1[$x];
return $c;
}, $Array2);
I like to compare an array from a database against another array to generate a new array of missing ids using isset or array_key_exists. Here is the array from my database I am creating.
foreach ($listings as $listing) {
$local_ids[$listing['id']] = 'true';
}
$local_ids = array(
'567' => true,
'568' => true,
'569' => true,
'570' => true,
'571' => true,
'572' => true,
'573' => true
);
$new_ids = array(
'568',
'569',
'572',
'573',
'574',
'575',
'576',
'577'
);
Taking the above two arrays, I would like to cycle through them to give me a result that 567, 570, 571 have been removed.
I altered the $new_ids as this list may or may not contain ids that are new and not in $local_ids. These can be ignored, I just need the ones removed.
i know a method which goes like this
<?php
$a1 = array("a" => "one", "two", "three", "four");
$a2 = array("b" => "one", "two", "three");
$result = array_diff($array1, $array2);
print_r($result);
?>
or
<?php
$a1 = array("a" => "one", "b" => "two", "c" => "three", "four");
$a2 = array("a" => "one", "two", "three");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
There is function array_values in PHP such that
$array2 = array_values($array1);
$array2 has the same values as $array1 but keys are from
0 to sizeof($array1) - 1. Is it possible to get mapping from old keys to new keys?
EDIT. I will explain on an example:
$array1 = array( 'a' => 'val1', 'b' => 'val1');
$array2 = array_values( $array1 );
so now array2 has next values
$array2[0] = 'val1'
$array2[1] = 'val2'
How get array3 such that:
$array3['a'] = 0
$array3['b'] = 1
To produce a key map you need to first get the keys into a regular array and then flip the keys and values:
$array1_keymap = array_flip(array_keys($array1));
For example:
$array1 = array(
'a' => 123,
'b' => 567,
);
$array1_values = array_values($array1);
$array1_keymap = array_flip(array_keys($array1));
Value of $array1_values:
array(
0 => 123,
1 => 567,
);
Value of $array1_keymap:
array(
'a' => 0,
'b' => 1,
);
So:
$array1['a'] == $array1_values[$array1_keymap['a']];
$array1['b'] == $array1_values[$array1_keymap['b']];
Yes, as simple as
$array2 = $array1;
In this case you would get both values and keys like they are in the original array.
$keyMapping = array_combine(array_keys($array1), array_keys($array2));
This the keys of $array1 and maps them to the keys of $array2 like so
<?php
$array1 = array(
'a' => '1',
'b' => '2',
);
$array2 = array_values($array1);
print_r(array_combine(array_keys($array1), array_keys($array2)));
Array
(
[a] => 0
[b] => 1
)
You can use:
$array3 = array_keys($array1);
Now $array3[$n] is the key of the value in $array2[$n] for any 0 <= $n < count($array1). You can use this to determine which keys were in which places.
If you want to keep the same value of array1 but change the key to index numbers, try this:
$array2 = array();
foreach ($array1 as $key => $value){
$array2[] = $value;
// or array_push($array2, $value);
}
var_dump($array2);