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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:23:17+00:00 2026-06-13T16:23:17+00:00

$array1 = array($name1 => $id1); $array2 = array($name2 => $id2, $name3 => $id3); I

  • 0
$array1 = array("$name1" => "$id1");

$array2 = array("$name2" => "$id2", "$name3" => "$id3");

I need a new array combining all together, i.e. it would be

$array3 = array("$name1" => "$id1", "$name2" => "$id2", "$name3" => "$id3");

What is the best way to do this?

Sorry, I forgot, the ids will never match each other, but technically the names could, yet would not be likely, and they all need to be listed in one array. I looked at array_merge but wasn’t sure if that was best way to do this. Also, how would you unit test this?

  • 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-13T16:23:18+00:00Added an answer on June 13, 2026 at 4:23 pm

    array_merge() is more efficient but there are a couple of options:

    $array1 = array("id1" => "value1");
    
    $array2 = array("id2" => "value2", "id3" => "value3", "id4" => "value4");
    
    $array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/);
    $array4 = $array1 + $array2;
    
    echo '<pre>';
    var_dump($array3);
    var_dump($array4);
    echo '</pre>';
    
    
    // Results:
        array(4) {
          ["id1"]=>
          string(6) "value1"
          ["id2"]=>
          string(6) "value2"
          ["id3"]=>
          string(6) "value3"
          ["id4"]=>
          string(6) "value4"
        }
        array(4) {
          ["id1"]=>
          string(6) "value1"
          ["id2"]=>
          string(6) "value2"
          ["id3"]=>
          string(6) "value3"
          ["id4"]=>
          string(6) "value4"
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any php function that do this: $source = array('id1'=>'name', 'id2'=>'name2', 'id3'=>'name3'); $keys
I have a PHP array, say, $array = Array(Name1,Name2,Name3,Name4,Name5); I want to find the
I have: $all = array( array('id'=>1, 'cat'=>'Main','type'=>'Name0'), array('id'=>2, 'cat'=>'Main','type'=>'Name1'), array('id'=>3, 'cat'=>'Main','type'=>'Name3'), array('id'=>4, 'cat'=>'Main','type'=>'Name4'), array('id'=>5,
a array like this: a = [<root>, <products>, <product>, <id>, 1, </id>, <name>, name1,
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
All, I have the following array in array format. The JSON format is shown
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},
$cars = array( array('name' => 'Toyota', price => 10000, ...), array('name' => 'Ford', price
I have an array generated by PHP array( array( 'name'=>'node1', 'id' => '4' ),
i have an array: Array ( [47] => Array ( [name] => 3543 good

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.