array_push for associative arrays array_push for associative arrays arrays arrays

array_push for associative arrays


This is your problem:

$newArray[$key] is null cause $newArray is an empty array and has not yet values.

You can replace your code, with

array_push( $newArray, $value );

or instead of array_push to use

$newArray[$key] = $value;

so you can keep the index of your $key.


I use array_merge prebuilt function for push in array as associative.

For example:-

$jsonDataArr=array('fname'=>'xyz','lname'=>'abc');$pushArr=array("adm_no" => $adm_no,'date'=>$date);$jsonDataArr = array_merge($jsonDataArr,$pushArr);print_r($jsonDataArr);//Array ( [fname] => xyz [lname] => abc [adm_no] =>1234 [date] =>'2015-04-22')