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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:13:33+00:00 2026-06-11T06:13:33+00:00

Just for curiosity (I know it can be a single line foreach statement), is

  • 0

Just for curiosity (I know it can be a single line foreach statement), is there some PHP array function (or a combination of many) that given an array like:

Array (
    [0] => stdClass Object (
        [id] => 12
        [name] => Lorem
        [email] => lorem@example.org
    )
    [1] => stdClass Object (
        [id] => 34
        [name] => Ipsum
        [email] => ipsum@example.org
    )
)

And, given 'id' and 'name', produces something like:

Array (
    [12] => Lorem
    [34] => Ipsum
)

I use this pattern a lot, and I noticed that array_map is quite useless in this scenario cause you can’t specify keys for returned array.

  • 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-11T06:13:35+00:00Added an answer on June 11, 2026 at 6:13 am

    Just use array_reduce:

    $obj1 = new stdClass;
    $obj1 -> id = 12;
    $obj1 -> name = 'Lorem';
    $obj1 -> email = 'lorem@example.org';
    
    $obj2 = new stdClass;
    $obj2 -> id = 34;
    $obj2 -> name = 'Ipsum';
    $obj2 -> email = 'ipsum@example.org';
    
    $reduced = array_reduce(
        // input array
        array($obj1, $obj2),
        // fold function
        function(&$result, $item){ 
            // at each step, push name into $item->id position
            $result[$item->id] = $item->name;
            return $result;
        },
        // initial fold container [optional]
        array()
    );
    

    It’s a one-liner out of comments ^^

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

Sidebar

Related Questions

This is just a curiosity question. I know that Spring can be used in
I know this is a stupid question. But just out of curiosity, is there
I'm just asking this out of curiosity : Is there any tool that can
Just out of curiosity: I know I can tell the compiler if I want
This is just out of curiosity. Is there any way we can make an
Just out of curiosity, wanted to know if there is a need to use
Just for curiosity... If possible, where can I find and see the javascript submit()
Is there any way (just out of curiosity because I came across multiple same-value
Just a question for my own curiosity. I have heard many times that it's
I know this question could be in vain, but it's just out of curiosity,

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.