Ad Code

Ticker

6/recent/ticker-posts

Insert array into another array on specific position

Insert array into another array in PHP

Insert array into another array on specific position


There are different methods to insert an element or array in another array which is not matter indexed array or associative array. This post has multiple methods you can use anyone from this methods.

$frst_arr=array('1','2','3');
$fnl_arr=array('A', 'B', 'C');
array_push($fnl_arr, $frst_arr);

print_r($fnl_arr); //Final array

This is the simplest method to merge two different arrays into one array. According to this method, both arrays are combined into one array ($fnl_arr), and all elements are arranged after the last elements in the final array.


If you are inserting elements into an array then you can use index and index name in string and just line array variable name and brackets[]. For example

$num_arr=array('1','2','3','4'); //0,1,2,3 indexes in this array
$num_arr[4]=5;                   //on 4th index number insert 5
// if you are using associative array then use this method for insert
$str_arr=array('name'=>'Andi', 'city'=>'London');
$str_arr['dob']='20-04-2010';       //add dob index and value


Post a Comment

0 Comments

Ad Code