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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:02:16+00:00 2026-05-11T07:02:16+00:00

I want to do something seemingly very simple, but I can’t find anything about

  • 0

I want to do something seemingly very simple, but I can’t find anything about it: simply extract a subset of an array similar to array_splice, but using keys to retrieve the values :

$data = array('personName' => 'John', 'personAge' => 99, 'personId' => 1,                 /* many more data I don't need here ... */);  list($name, $age, $id) = array_splice_by_keys($data,                           array('personName', 'personAge', 'personId'); 

If all else fails, isn’t there a builtin function to filter an associative array by keys? For example:

$filteredArray = array__extract__keys__and__values($srcArray, $arrayOfWantedKeys);

// create a new array with ONLY those key => values I need $wanted_values = array_extract_keys_and_values($data,                   array('personName', 'personAge', 'personId');  echo $wanted_values['personName'];   

I guess the reason why I want to do the first one, is that I don’t like to repeat associative array access all over my code, it would seem better optimized to copy the values that are used a lot (in a loop for example), into a local variable, plus it’s much easier to type $name than $somearray[‘name’].

EDIT: Thanks, I guess for use with list, the solution would be

list($x, $y, $z) = array_values(array_intersect_key($array, array_flip($wantedKeys))); 

Intesresting use of array_flip!

  • 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. 2026-05-11T07:02:17+00:00Added an answer on May 11, 2026 at 7:02 am

    in php version >= 5.1.0 you could use array_intersect_key:

    $data = array('personName' => 'John', 'personAge' => 99, 'personId' => 1,             'test' => 23); $ex = array('personName'=>0, 'personAge'=>0, 'personId'=>0); var_dump(array_intersect_key($data, $ex)); 

    values in $ex don’t matter, they just have to be present.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A seemingly silly question but I can't seem to find a definitive answer one
I'm having trouble with a seemingly very simple problem: I want to get a
I'm trying to do something seemingly simple. I want to return a column value
I'm stuck on a RegEx problem that's seemingly very simple and yet I can't
I can't believe I'm asking this, but can't find a clear answer about it.
I want something like the following but would like it to be reusable for
I want something like a canvas, but where i'd be able to manipulate pixels
I want to extract information of bullets present in word document. I want something
So I'm trying to achieve something seemingly very basic in jQuery. I have a
Every time I try to do something seemingly-simple in CSS, it doesn't work. I

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.