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

  • SEARCH
  • Home
  • 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 9007079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:35:19+00:00 2026-06-16T01:35:19+00:00

I have two arrays. $keys = array(1,2,3,6,9); //note that 9 does not exist as

  • 0

I have two arrays.

$keys = array(1,2,3,6,9); //note that 9 does not exist as key in the $values array
$values = array('000', '001', '002', '003', '004', '005', '006', '007');

What I would like to do is combine these two arrays in such way that I will get an array which is filtered by the $keys array. You could say that the $values array is reflected to the $keys array which returns a filtered array where only the $values appear if the key of the $values is equal to one of the values in the $keys array.

The question is hard to explain in words so the equivalent of my question above is in the foreach down here:

foreach ($keys as $k){
    if (!array_key_exists($k, $values)){ continue; } //prevents $k=9 as key in the $values array
    $new[$k] = $values[$k];
}
print_r($new); // Array ( [1] => 001 [2] => 002 [3] => 003 [6] => 006 )

I think it should be possible with a combination of array_merge, array_combine, array_diff and/or array_unique but I can’t seem to figure out the solution. It might be possible that the foreach above is the best way to do this, but I can imagine there must be another great way to do this. So my question basically is: is there another way to accomplish the same goal with less code?

I know what keys exist in the $values array, but I have to reflect those to the $values in a nice way.

$keys_in_values = array_intersect($keys, array_keys($values));
// Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 6 )

Now you might be asking why I am not just using the foreach. Well, the answer is fairly simple. Because I don’t want to. I seems 100% right, but I am just curious if there are other/better solutions. I have some more advanced pieces of code where I would like to use this (recursively) but that is a bit too much for this question since the answer will be the same.

  • 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-16T01:35:21+00:00Added an answer on June 16, 2026 at 1:35 am

    All you need is array_intersect_key + array_flip

    $data = array_intersect_key($values, array_flip($keys));
    var_dump($data);
    

    Output

    array (size=4)
      1 => string '001' (length=3)
      2 => string '002' (length=3)
      3 => string '003' (length=3)
      6 => string '006' (length=3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays, both have the same keys (different values) however array #2
I have two arrays the first one has this structure Parameter Keys array array
I have two arrays: $array1 = array(1=>1,10=>1,12=>0,13=>13); $array2 = array(1=>Hello,10=>Test,12=>check,13=>error); Here $array1 has keys
I have built a custom hashmap using two arrays. One contains keys another values.
I have two arrays of values that I would like to combine - but
I have two arrays of arrays that have the keys 'menu_title' and 'id'. I
Suppose we have two arrays: $a=array('1'=>'Apple','2'=>'Microsoft', '3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple'); $b=array(1,3); Where $b array represents the keys
Possible Duplicate: Merge 2 Arrays and Sum the Values (Numeric Keys) I have two
I have two arrays with matching keys and I need to merge the values
I have two arrays values and keys both of the same length. I want

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.