New values can be added in array using [] or using array_push as well.
using array_push:
$names = [
'John',
'Bryan',
'Sersi',
];
if(somecondition){
array_push($names, 'Bobby', 'Nail');
}
using [] you can add them one at a time:
$names = [
'John',
'Bryan',
'Sersi',
];
if(somecondition){
$names[] = 'Bobby';
$names[] = 'Nail';
}
A small comparison between array_push() and the $array[] method, the $array[] seems to be a lot faster.
<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
$array[] = $x;
}
?>
//takes 0.0622200965881 seconds
and
<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
array_push($array, $x);
}
?>
//takes 1.63195490837 seconds
so if your not making use of the return value of array_push() its better to use the $array[] way.
print_r(array_merge($names[0], $secondNames[0]));