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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:03:38+00:00 2026-05-29T05:03:38+00:00

I have two arrays: $arrKeys = array(‘str’, ‘str’, ‘otherStr’); $arrVals = array(‘1.22’, ‘1.99’, ‘5.17’);

  • 0

I have two arrays:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');

I would like to merge them into something like this

$arrResult = array(
    array('str' => 1.22),
    array('str' => 1.99),
    array('otherStr' => 5.17)
);

The keys are non-unique, otherwise I’d use array_combine. That would give a bit different output, but it would suit me as well.

Can this be done in an elegant way using PHP 5.2.x, without foreach/for loops, preferably using PHP’s built-in functions?

  • 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-29T05:03:40+00:00Added an answer on May 29, 2026 at 5:03 am

    You can use array_map:

    $arrKeys = array('str', 'str', 'otherStr');
    $arrVals = array('1.22', '1.99', '5.17');
    function foo($key, $val) {
       return array($key=>$val);
    }
    
    $arrResult = array_map('foo', $arrKeys, $arrVals);
    
    print_r($arrResult);
    Array
    (
        [0] => Array
            (
                [str] => 1.22
            )
    
        [1] => Array
            (
                [str] => 1.99
            )
    
        [2] => Array
            (
                [otherStr] => 5.17
            )
    
    )
    

    BTW, if you upgrade to PHP 5.3 you can do this using an anonymous function, which is a bit more elegant:

    array_map(function($key, $val) {return array($key=>$val);}, $arrKeys, $arrVals);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays, I want to merge these two arrays into single array.
I have two arrays of values that I would like to combine - but
I have two arrays of strings that I would like to compare for equality:
I have two Arrays of strings, and I would like to find the set
I have two arrays they look like $a1 = array( array('num' => 1, 'name'
I have two arrays that both look like this: Array ( [0] => Array
I have two arrays of hashes. I need to merge them together so the
I have two arrays of data that I would like to display in an
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids

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.