So I am a bit stuck here, I can do it with foreach loop but I want to find a cleverer way of doing it.
Update: There was something that I've missed in the question. The arrays may come in random order and in different length thus having different keys. Examples below updated.
Here is the case:
Array1
array (
slug1 => England,
slug2 => France,
slug3 => Italy,
slug4 => Germany,
)
Array2
array (
slug2 => 215,
slug1 => 168,
slug4 => 55,
slug5 => 149,
slug3 => 40,
slug6 => 137,
)
I want to intersect those arrays and build new one which has the following elements:
array (
168 => England,
215 => France,
40 => Italy,
55 => Germany,
)
Note: elements are not ordered though that could be achieved easily.
Answer to Original Question
You can use array_combine
it creates an array by using one array for keys and another for its values
$array1 = array(
"slug1" => "England",
"slug2" => "France",
"slug3" => "Italy",
"slug4" => "Germany");
$array2 = array(
"slug1" => "168",
"slug2" => "215",
"slug3" => "40",
"slug4" => "55");
$final = array_combine($array2, $array1);
echo "<pre>";
print_r($final);
Output
Array
(
[168] => England
[215] => France
[40] => Italy
[55] => Germany
)
Answer to Updated Question
Update: There was something that I've missed in the question. The arrays may come in random order and in different length thus having different keys. Examples below updated.
$array1 = array(
"slug1" => "England",
"slug2" => "France",
"slug3" => "Italy",
"slug4" => "Germany");
$array2 = array (
"slug2" => 215,
"slug1" => 168,
"slug4" => 55,
"slug5" => 149,
"slug3" => 40,
"slug6" => 137);
$final = customCombine($array2, $array1);
echo "<pre>";
print_r($final);
Output
Array
(
[215] => France
[168] => England
[55] => Germany
[40] => Italy
)
Function Used
function customCombine($keys, $arr) {
$t = array();
foreach ( $keys as $k => $val ) {
isset($arr[$k]) and $t[$val] = $arr[$k];
}
return $t;
}
use array_combine()
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
Baba's answer should work fine for you, but here's an interesting way to deal with different key orders between both arrays and/or different sizes.
// get values from $array2 in the order in which they appear in $array1
// whereby the array keys are used to match them
$keys = array_intersect_key($array2, $array1);
// create a new array with the keys found in the previous step and
// another array_intersect_key() to only select the matching items
// from $array1
array_combine($keys, array_intersect_key($array1, $keys));
It also makes sure that array_combine()
works with same sized arrays; the size of $array2
is the output size.
I found the best solution for me:
<?php
$array1 = array("slug1" => "England","slug2" => "France","slug3" => "Italy","slug4" => "Germany");
$array2 = array("slug1" => "168","slug2" => "215","slug3" => "40","slug4" => "55", "slug5" => "178");
ksort($array1);
ksort($array2);
$test1 = array_intersect_key($array1, $array2);
$test2 = array_intersect_key($array2, $array1);
$final = array_combine($test2, $test1);
print_r($final);
?>
To expand Jack's answer, as it might combine the arrays in the order they're constructed in and not the order the keys are matching:
Array
(
[215] => England
[168] => France
[55] => Italy
[40] => Germany
)
Some intermediate data juggling can sort it out (and it works no matter which array is shorter):
$array1 = array(
'slug1' => 'England',
'slug2' => 'France',
'slug3' => 'Italy',
'slug4' => 'Germany'
);
$array2 = array (
'slug2' => 215,
'slug1' => 168,
'slug4' => 55,
'slug5' => 149,
'slug3' => 40,
'slug6' => 137
);
$keys = array_intersect_key($array2, $array1);
ksort($keys);
$intersect = array_intersect_key($array1, $keys);
ksort($intersect);
$final = array_combine($keys, $intersect);
print_r($final);
Outputs
Array
(
[168] => England
[215] => France
[40] => Italy
[55] => Germany
)