Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Is array_merge perform reindexing?

Suppose I have an associative array with keys that are alphabetic string, and if I merge something to this array, It will merge successfully without reindexing like

$arr1 = array('john'=>'JOHN', 'marry'=>'Marry');
$arr1 = array_merge(array('78'=>'Angela'),$arr1);
print_r($arr1);

then this will correctly merge new component to array and its output will be

Array
(
    [0] => Angela
    [john] => JOHN
    [marry] => Marry
)

But when I tried same thing like this

 $arr1 = array('34'=>'JOHN', '04'=>'Marry');
 $arr1 = array_merge(array('78'=>'Angela'),$arr1);
 print_r($arr1);

then its output is like this

Array
(
    [0] => Angela
    [1] => JOHN
    [04] => Marry
)

Can anyone describes this scenario..... Also I want my array to be like this after merging..

Array
    (
        [78] => Angela
        [34] => JOHN
        [04] => Marry
    )

How can I Achieve that??

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

as per definition array_merge will reindex numeric indexes. a string with a numeric value is also a numeric index.

To prevent this behaviour concatenate the arrays using $arr1+$arr2

about 4 years ago · Santiago Trujillo Report

0

You don't need to use array_merge() as you can simply add the arrays:

$arr1 = [
  '10' => 'Angela',
  'john' => 'JOHN',
  'marry' => 'Marry',
];

$arr2 = [
  '78' => 'Angela'
];

$arr3 = $arr2 + $arr1;

array_merge() - ... values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!