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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:01+00:00 2026-05-27T20:47:01+00:00

I had a head-scratcher yesterday. Basically I had an associative array of some data

  • 0

I had a head-scratcher yesterday.

Basically I had an associative array of some data with string keys(containing numeric values, but still, quoted strings) to go in a <select> box as options. I wanted to prepend a placeholder value to the select box’s options.

I attempted to use array_merge:

$placeholder = "Month";
$source = array
(
    '01' => '01 - January',
    '02' => '02 - February',
    '03' => '03 - March',
    '04' => '04 - April',
    '05' => '05 - May',
    '06' => '06 - June',
    '07' => '07 - July',
    '08' => '08 - August',
    '09' => '09 - September',
    '10' => '10 - October',
    '11' => '11 - November',
    '12' => '12 - December'
);

$source = array_merge(array('' => $placeholder), $source);

And I was seeing weird results — the resultant $source array was similar to the following:

(
    '' => 'Month',
    '01' => '01 - January',
    '02' => '02 - February',
    '03' => '03 - March',
    '04' => '04 - April',
    '05' => '05 - May',
    '06' => '06 - June',
    '07' => '07 - July',
    '08' => '08 - August',
    '09' => '09 - September',
    0 => '10 - October',
    1 => '11 - November',
    2 => '12 - December'
);

Note that it began using what appear to be auto-incrementing integer keys for October through December for some reason.

I can replace the array_merge call with this:

$source = array('' => $placeholder) + $source;

And the array doesn’t get rekeyed and everything is fine, but I don’t understand what’s happening under the scenes in the array_merge case.

Can anyone please explain what was happening to my array keys when I used array_merge? Thanks!

  • 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-27T20:47:02+00:00Added an answer on May 27, 2026 at 8:47 pm

    Problem doesn’t come from the array_merge() function.

    If you print $source before merging, you’ll have this :

    array(12) {
      ["01"]=>
      string(12) "01 - January"
      ["02"]=>
      string(13) "02 - February"
      // ...
    }
    

    Try to rename your first key '01' to '1', You will have the following return :

    array(12) {
      [1]=>
      string(12) "01 - January"
      ["02"]=>
      string(13) "02 - February"
      // ...
    }
    

    PHP auto convert your key to integer. But I can’t say you why It does it.

    EDIT :

    I have found the answer on php.net (PHP.net) :

    A key may be either an integer or a string. If a key is the standard
    representation of an integer, it will be interpreted as such (i.e. “8”
    will be interpreted as 8, while “08” will be interpreted as “08”).
    Floats in key are truncated to integer. The indexed and associative
    array types are the same type in PHP, which can both contain integer
    and string indices.

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

Sidebar

Related Questions

I thought I had this mess sorted out in my head but for some
I had some time this afternoon to run a head to head comparison between
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
I had some problem to load Class object with this code: <html> <head> <meta
I followed THIS but had no luck. My . jsp file has in head
I have a tricky problem that has had me scratching my head for a
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had a good search here but can't see anything that gets my mind in
I had a discussion with some colleagues mentioning that there are not too many
I've had it drilled into my head many many times that if a copy-constructor

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.