Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:16+00:00 2026-06-14T08:49:16+00:00

So I am a bit stuck here, I can do it with foreach loop

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:49:17+00:00Added an answer on June 14, 2026 at 8:49 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit stuck on what is probably a common situation, but can't find
I am a bit stuck here and can't think further. public struct CandidateDetail {
Im a bit stuck with my lighting. My light can light up meshes but
As you can tell I'm new to multithreading and a bit stuck here. For
bit stuck try to achieve something in Jquery and wondered if anyone can assist..
I'm a bit stuck repairing a faulty table (on Hbase 0.92.1-cdh4.0.0, Hadoop 2.0.0-cdh4.0.0) There
I am once again a bit stuck in my practising. I want an MP3
I'm trying to create a custom search but getting stuck. What I want is
I am a bit stuck on how I should model this out. Here is
I'm a little bit stuck with the new ContactsContract-API here. I think I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.