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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:17:20+00:00 2026-05-21T02:17:20+00:00

I would like to do something similar to the following. $ind=array(‘a’,’b’,’c’); $arr[‘a’]=1; $arr[‘b’]=4; $arr[‘c’]=7;

  • 0

I would like to do something similar to the following.

$ind=array('a','b','c');
$arr['a']=1;
$arr['b']=4;
$arr['c']=7;
$d= $arr[$ind];
print_r($d);

It obviously doesnt work and I was wondering if there is still a simple way to do it in 1 or two lines of code.

EDIT

Here is a better example to demonstrate what I mean.

$ind=array('a','c','b','d','e','a','a');
$arr['a']=1;
$arr['b']=4;
$arr['c']=7;
$d=$arr[$ind];

Now I want to receive that $d is equal to 1,7,4,1,1

array_intersect_key doesnt work. Or at least I cant make it work for me. I was thinking array_walk and a lamda function or something. Anyone?

NOTE: This syntax is available in Matlab (well, almost). So you could for example type:

arr=rand(20,1);
ind=[1 2 10 2 1 1];
newArr=arr(ind); 

and it would produce vector of length 6 with the values as indexed by the ind vector.

  • 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-21T02:17:20+00:00Added an answer on May 21, 2026 at 2:17 am

    To get the items from $arr which have keys matching the values in $ind, you can use the following:

    $arr = array('a' => 1, 'b' => 4, 'c' => 7);
    $ind = array('a','c');
    $d   = array_intersect_key($arr, array_flip($ind));
    // $d = array('a' => 1, 'c' => 7);
    

    The call to array_flip() simply “flips” the keys/values in $ind (to make it look like array('a'=>0,'c'=>1)) then we find the keys intersection with array_intersect_key() which returns the items from the first array ($arr) which have keys matching the second array (the flipped $ind).


    Edit Building on mario’s answer (which is broken at the time of writing, and doesn’t do what you appear to want) you could use

    class MultiIndex extends ArrayObject
    {
        public function offsetGet($offset)
        {
            if (is_array($offset)) {
                $return = array();
                foreach ($offset as $o) {
                    if ($this->offsetExists($o)) {
                        $return[] = parent::offsetGet($o);
                    }
                }
                return $return;
            } else {
                return parent::offsetGet($offset);
            }
        }
    }
    
    $arr = new MultiIndex($arr);
    $d = $arr[$ind];
    // $d = array(1,7,4,1,1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write something similar to the following: // I will pass
I would like to achieve something similar to the following in Guice: public MyClass
I'd like to create DetachedCriteria which would perform something similar to the following SQL
I would like to implement something similar to a c# delegate method in PHP.
I would like to implement something similar to 37Signals's Yellow Fade effect. I am
I would like to achieve something similar to how scala defines Map as both
I would like to perform something similar to this (ie get the sum of
I would like to achieve something very similar to Microsoft Access query designer -
I have already posted something similar here but I would like to ask the
Hi I would like to know if its possible to do something similar to

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.