0

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']);
}

1 Answer 1

4

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;   
}
Sign up to request clarification or add additional context in comments.

3 Comments

That seems to just change the order of the resultArray ( [Date] => 2013 06 10 [0] => Array ( [Post_timestamp] => 1370876787 ) )
are you sure you didn't miscopy $rows["Date"] = date(... as $array["Date"] = date(...
yes I did! when I saw your answer I thought right away, that's the answer, and when it didn't work I didn't understand. So simple. Big help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.