1

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.

5 Answers 5

2

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
)

See Live Demo

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

An additional ksort() on both arrays may be needed if the order of the keys is not guaranteed to be identical.
OK that looks very good but if the arrays have different length this won't work, I know this was not mentioned but that's the curent case
@infinity that what not in your question but ... Update your question with an example i'll update my answer with the solution
@Baba I know +1 for your answer so far it's exact match
2

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.

6 Comments

Nice but array_slice would make it fail .. example eval.in/3583 it returns [215] => England , [55] => France instead of [55] => Germany , [215] => France`
I think I'll need to sort both array before doing that, otherwise it'll get wrong keys/values, as @Baba pointed
ksort would not work .. see eval.in/3583 use ksort already the main issue is array_slice i'll update his question with the correct implementation
@Baba Fixed, the solution is kinda funky I must admit :)
@infinity As Baba mentioned, the ksort() won't do any good; once the arrays are of different size you're pretty much screwed :) updated my answer, it should work as expected now.
|
1

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
)

Comments

0

use array_combine()

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

https://www.php.net/array-combine

Comments

0

I found the best solution for me:

http://eval.in/3578

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

1 Comment

Should be no different than my answer. Btw, the double ksort() is not really needed here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.