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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:30+00:00 2026-05-27T19:10:30+00:00

I need to combine the keys from an array, with the values in another

  • 0

I need to combine the keys from an array, with the values in another array:

$a = array(4=>2000,5=>5000,7=>1000,3=>5000);
$b = arrray(array(0=>0,1=>4,2=>10,3=>1000),array()...)

This is what I’ve written (not working):

$keys = array_keys($a);
foreach ($b as $k => $v) {
array_combine($keys,$v);
}

What I expect to get is:

$c = array(array(4=>0,5=>4,7=>10,3=>1000),array(4=>...));
  • 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-27T19:10:31+00:00Added an answer on May 27, 2026 at 7:10 pm

    Since b has multiple arrays in it, you will just need to do a simple iteration of b:

    $a = array(4=>2000,5=>5000,7=>1000,3=>5000);
    $b = array(array(0=>0,1=>4,2=>10,3=>1000), array(0=>0,1=>4,2=>10,3=>1000));
    
    $keys = array_keys($a);
    $length = count($keys);
    $c = array();
    foreach($b as $sub) {
        if(!is_array($sub)) {
            $sub = array();
        }
    
        $c[] = array_combine(array_pad($keys, count($sub), NULL), array_pad(array_values($sub), $length, NULL)); // Pad removes the possibility of a warning
    }
    
    print_r($c);
    

    You had the right idea with array_keys($a) and array_combine(). The other argument you needed to feet into array_combine() is the values, array_values($b). But since there are multiple arrays in b, you need to loop through each. As a failsafe in case there are more or less key/value pairs in a or any of the sub-arrays of b, I have added array_pad() to make sure that the two arrays passed in to array_combine() are of equal length so that PHP doesn’t complain.

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

Sidebar

Related Questions

I need to extract data from a .mpp file on the network and combine
I have two arrays I want to combine. I need to take the values
I need to combine rows from two Excel worksheets into one long row, based
I need to combine different row's values in table in oracle. For example, consider
I need to combine the price with USD but I am getting this error.
I need to combine to a string column and the result of a row_number()
I have 3 byte arrays in C# that I need to combine into one.
I have a MySql database with three tables I need to combine in a
I need to create and combine several expressions for child entity into one to
I need to convert the function path::combine(path1, path2). Please help me if you have

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.