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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:40:53+00:00 2026-05-20T12:40:53+00:00

Pretty straightforward: // turn array(‘foo’, ‘bar’, ‘hello’, ‘world’); // into array(‘foo’ => ‘bar’, ‘hello’

  • 0

Pretty straightforward:

// turn
array('foo', 'bar', 'hello', 'world');

// into
array('foo' => 'bar', 'hello' => 'world');

Right now I’m using:

do{
    $arrayOut[current($arrayIn)] = next($arrayIn);
}while(next($arrayIn));

I’m wondering if there’s a way to do it without the intermediary variable, $arrayOut. I could write a function, however this is a single use case, and I’m trying to keep my script uncluttered. I’m just wondering if there’s something I missed in the docs that would serve this purpose.


The values are coming from a routing path:

route/to/controller/action/key1/value1/key2/value2

It’s exploded, and eventually after using the other components, I’m left with ('key1', 'value1', 'key2', 'value2', ...)


Thank you folks for the insight and suggestions. Long Ears won this one for the concise approach, which when expanded to more than “1 line”, is not terribly cryptic (I don’t think at least)

However, also with regard to Long Ears‘ suggestion, perhaps my desire for semantically precise code with minimal verbosity has gotten the better of me, and I was chasing daisies trying to keep my variable scope “pollutant-free”, to paraphrase myself.

  • 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-20T12:40:53+00:00Added an answer on May 20, 2026 at 12:40 pm

    You can make your existing code slightly more efficient (removes one function call per iteration, for one at the beginning:)

    $b = array();
    array_unshift($a, false);
    while (false !== $key = next($a)) {
        $b[$key] = next($a);
    }
    

    Ok, (shudder) here’s your one liner:

    $b = call_user_func_array('array_merge', array_map(function($v) { return array($v[0] => $v[1]); }, array_chunk($a, 2)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this jQuery countdown plugin here and it seems pretty straightforward but
Pretty straightforward stuff, here -- I'm just not good enough with mysql to understand
Pretty straightforward, but I just want to know which is faster. I think simply
I'm writing a pretty straightforward e-commerce app in asp.net , do I need to
My question is pretty straightforward: You are an executable file that outputs Access granted
For the following pretty straightforward task: query a list of products from a DB
Shouldn't this be a pretty straightforward operation? However, I see there's neither a size()
Recently I converted a Swing application to Webstart. The process was pretty straightforward, but
OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward,
This is a pretty straight forward attempt. I haven't been using python for too

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.