Array_merge changes keys

I got the following array:

$arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2'); 

The problem is that when I use array_merge( (array) "Select the data", $arr); , it changes the keys of the array to:

 Array ( [0] => Not specified [1] => Somedata [2] => Somedata1 [3] => Somedata2 ) 

Is it possible to skip the array_merge key prefix so that the result looks like this:

 Array ( [0] => Not specified [6] => Somedata [7] => Somedata1 [8] => Somedata2 ) 
+4
source share
1 answer

Use + operator to create a union of two arrays:

 $arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2'); $result = (array)'Select the data' + $arr; var_dump($result); 

Result:

 array(4) { [0]=> string(15) "Select the data" [6]=> string(8) "Somedata" [7]=> string(9) "Somedata1" [8]=> string(9) "Somedata2" } 
+8
source

Source: https://habr.com/ru/post/1433914/


All Articles