Tags
PHP , array
Asked 3 years ago
9 Jun 2021
Views 2331
ruby-rails

ruby-rails posted

Copy array items into another array in PHP


How to Copy array items into another array in PHP?
Jaylan

Jaylan
answered Aug 22 '21 22:49

array_merge() function be used to copy one arry to another and create new array

<?php 
$n1=array("1","2"); 
$n2=array("3","4");
$n=array_merge($n1,$n2);
print_r($n);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

?>
Jaylan

Jaylan
answered Aug 22 '21 23:13

you can use for or foreach loop to copy all elements to another array in PHP


$n1=array("one","tw0"); 
$n2=array("three","four");
foreach($n2 as $val){
   $n1[]=$val;
}

print_r($n1);

Post Answer