I believe that this will have the effect that you're looking for. We are passing in the original array as the reference and it adds whatever data you pass to it to the original array.
function add_element (&$original_array = array(), $data) {
// Cast an array if it isn't already
!is_array($data) ? (array)$data : null;
if(!empty($data)) {
$original_array = $original_array + $data;
}
return true;
}
$names_array = array("first_name" => "bob");
$data_to_add = array("second_name" => "fred");
// Add new variable
add_element($names_array, $data_to_add);
// Show the contents
print_r($names_array);
See it live here: http://www.tehplayground.com/#DJXofIeQK
However, I have just taken what you posted as a starting point there. The above is basically the same as the following, which requires no special function:
$names_array = array("first_name" => "bob");
$data_to_add = array("second_name" => "fred");
// Add new variable
$names_array = $names_array + $data_to_add;
// Show the contents
print_r($names_array);
See it here: http://www.tehplayground.com/#PAbhOHaPT
array_push()to add new things to an array.$arr = array('0'=>'zero','1'=>'one','2'=>'two');I need to add$element = array('21'=>'twentyone','45'=>'fourthy-five');Whenarray_push($arr,$element);It creates extra index[3][3]=>array([21]...I need[2]=>two,[21]=>twentyone..