Reputation: 833
I create a simple associative array in a WHILE loop. The key is the field name and the value is a Unix timestamp. I would like to add to this array a new key=>value pair 'Date"=>format the Unix timestamp, json_encode the array and return it to a JQ script in an Ajax call. The array looks like this:
Array
( [0] => Array
( [Post_timestamp] => 1370876787 ) [Date] => 2013 06 10 )
However, shouldn't it look like this:
Array
( [0] => Array ( [Post_timestamp] => 1370876787 [Date] => 2013 06 10))
I guess my question is "how do I create the array so that the formatted timestamp and the raw timestamp are a single record"? Right now, it appears as if they are two records.
PHP
$query = "SELECT Post_timestamp FROM Comments LIMIT 1";
$result = mysqli_query($dbc, $query);
while ($rows = mysqli_fetch_assoc($result)) {
$array[] = $rows;
$array['Date'] = date("Y m d", $rows['Post_timestamp']);
}
Upvotes: 0
Views: 5753
Reputation: 24655
The problem is you have two different values into the array, what you need to do is push an array that contains both values. This should get you what you want.
$query = "SELECT Post_timestamp FROM Comments LIMIT 1";
$result=mysqli_query($dbc,$query);
while ($rows = mysqli_fetch_assoc($result)) {
$rows["Date"] = date("Y m d",$rows['Post_timestamp']);
$array[] = $rows;
}
Upvotes: 4