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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:19:50+00:00 2026-05-26T07:19:50+00:00

This is sort of two questions, but there may be one overall answer for

  • 0

This is sort of two questions, but there may be one overall answer for the whole problem. I have an array that I need to append onto another array. Both arrays must have specific numeric keys. My problems are:

  1. I need the numeric keys for the array that I am appending onto to be preserved.
    • array_splice() and array_merge() won’t work to join the arrays because numeric keys in both arrays will be reset.
  2. I need to make the keys of the newly added elements to be n through n + x, meaning if n is 100 and x is 25, the keys for the newly added elements should be 100 through 125.

Can anyone think of a somewhat efficient way of doing this?

EDIT

For anyone curious, found a better way of adding the correct keys to the array.

// add correct keys
$array_segment = array_combine(range($offset, $offset + count($array_segment) - 1), $array_segment);
// merge arrays while maintaining keys
$first_array = $first_array + $array_segment;
  • 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-26T07:19:51+00:00Added an answer on May 26, 2026 at 7:19 am

    I think this is a very simple solution but, if I understand well what you want, it works and it’s fast. In my opinion you can use this approach:

    $array1 = array(1 => 'a', 2 => 'b', 3 => 'c');
    $array2 = array(4 => 'd', 5 => 'e', 6 => 'f');
    
    foreach($array2 as $key => $value)
       $array1[$key] = $value;
    
    var_dump($array1);
    
    $array1[] = 'g';
    $array1[] = 'h';
    
    var_dump($array1);
    

    You can see the result here:

    http://codepad.org/ogD9drpK

    Another way which will avoid the foreach is to execute this instruction:

    // avoid the loop
    //foreach($array2 as $key => $value)
    //   $array1[$key] = $value;
    $array1 += $array2;
    

    You can see the result here:

    http://codepad.org/cZxCfRn6

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

Sidebar

Related Questions

I know there are questions on here similar to this, but I have not
I have a feeling that this is a subjective question, but perhaps there's something
Scala programmer should have known that this sort of writing : class Person{ var
OK, this may just me being ignorant, but I have the following route in
I'm a little shocked that (if?) this precise questions hasn't been asked, but 15
I know there are several questions named like this, but they don't seem to
There were few questions related to this but none answered the question in my
how to sort a list in Scala by two fields, in this example I
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I know this sort of code is not best practice, but nevertheless in certain

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.