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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:31:39+00:00 2026-06-06T23:31:39+00:00

I have two arrays with numeric keys. This is a small example: $test_a =

  • 0

I have two arrays with numeric keys. This is a small example:

$test_a = array(1 => 'one', 2 => '二');
$test_b = array(2 => 'two', 4 => 'four');

I want to merge them, with array_merge(), to receive $test_c array:

$test_c = array(
    1 => 'one',
    2 => 'two',
    4 => 'four'
);

The manual for array_merge mentions that:

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

what seems to be true, because this:

$test_c = array_merge($test_a, $test_b);
var_dump($test_c);

returns:

array (size=4)
    0 => string 'one' (length=3)
    1 => string '二' (length=3)
    2 => string 'two' (length=3)
    3 => string 'four' (length=4)

What did I try:

  1. I tried casting keys as strings:

    foreach($test_a as $key => $value) $test_a[(string)($key)] = $value;
    

    …keys are still numeric.

  2. I tried with strval():

    foreach($test_a as $key => $value) $test_a[strval($key)] = $value;
    

    no change. Keys are still numeric.

  3. I tried with this trick:

    foreach($test_a as $key => $value) $test_a['' . $key . ''] = $value;
    

    Does not work either. The keys are still numeric.

What surprised me, was when I found out that there is something like automatic conversion from string to numeric on array keys. This changed the keys to strings:

foreach($test_a as $key => $value) $test_a[' ' . $key . ' '] = $value;

and when I added trim():

foreach($test_a as $key => $value) $test_a[trim(' ' . $key . ' ')] = $value;

the keys were converted back to numeric.

Basically, I want to merge these two arrays. I guess that the only solution is to find a way to convert the keys from numeric to string data type.
If you can additionally explain the “automatic conversion” thing then I will be fully satisfied.

  • 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-06T23:31:41+00:00Added an answer on June 6, 2026 at 11:31 pm

    You could use the + operator on both arrays, like so:

    $test_a = array(1 => 'one', 2 => '二');
    $test_b = array(2 => 'two', 4 => 'four');
    
    var_dump( $test_b + $test_a);
    

    This will output:

    array(3) {
      [2]=>
      string(3) "two"
      [4]=>
      string(4) "four"
      [1]=>
      string(3) "one"
    }
    

    It’s not in the exact order you’re looking for, but you could use ksort() to sort by the keys to get the exact output. What you’re referring to as “automatic conversion” is actually called type coercion, and is referred to as type juggling in PHP.

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

Sidebar

Related Questions

im trying to merge two arrays together. both have numeric keys and are unique.
Possible Duplicate: Merge 2 Arrays and Sum the Values (Numeric Keys) I have two
I have two arrays a={a,b,c,d} b={1,2,3,4} now I want to create a 3rd array
I have two integer arrays which contain numeric values. I want to look through
I have two arrays with matching keys and I need to merge the values
I have two arrays, one is a node-node-cost array [a_node,b_node,cost] which has 8000 entries
I have a boolean array call it flag . I have two numeric arrays
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays. One looks like this: var array_one = [.a, .b, .c,
I have two arrays. I'd like to copy ranges of data from one of

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.