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 7661663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:37:41+00:00 2026-05-31T13:37:41+00:00

$array1 = [‘key1’=>’val1’, ‘key2’=>’val2’, ‘key3’=>’val3’] $array2 = [‘key1′,’key3’] I want to get all elements

  • 0
$array1 = ['key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3']
$array2 = ['key1','key3']

I want to get all elements from array1 that has keys in array2.
So the result should be:

$result = ['key1'=>'val1', 'key3'=>'val3']

I tried array_intersect_key($array1, $array2) but that doesn’t work. What is the fastest way to get the above result?

Edit: Forgot to mention that array2 maybe a associative array or single dimension.

  • 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-05-31T13:37:42+00:00Added an answer on May 31, 2026 at 1:37 pm

    You need to make key1 and key2 the keys in the second array before you can use array_intersect_key(): use array_flip() to do that

    $result = array_intersect_key($array1,array_flip($array2));
    

    EDIT

    It doesn’t matter if $array2 is associative or not:

    $array1 = array('key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3');
    $array2 = array('first'=>'key1','second'=>'key3');
    
    $filteredData = array_intersect_key($array1,array_flip($array2));
    var_dump($filteredData);
    

    still gives

    array
      'key1' => string 'val1' (length=4)
      'key3' => string 'val3' (length=4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've two arrays array1 and array2 and I want to add all elements of
Consider the following example: function async_callback(array1, array2, array3) { // All arrays are equal
Say I have NSMutableArray *array1 with 10 objects. I want to create an *array2
I have two arrays array1 and array2. I want to merge these two arrays
I have 2 arrays: @array1 = [a,b,c,d,e] @array2 = [d,e,f,g,h] I want to compare
I want to divide array into specific chunks Like array1 = 24%; array2 =
Data in this fashion: [Array1] = ['blue','green', 'yellow','red','very very red'] [Array2] = ['ColumnA','ColumnB','ColumnC','ColumnD','ColumnD'] This
I have two JavaScript arrays: var array1 = [Vijendra,Singh]; var array2 = [Singh, Shakya];
I am ruby beginner. the case example: array1: ['A', 'ww','g'] array2: ['dsdsdsds','A', 'ww','jgjhgj', 'g']
var array1:Array = new Array(); var array2:Array = new Array(); var obj1:Object = new

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.