0

This is a working example of my query Retrieve data as JSON using PHP:

$result = mysql_query("SELECT unix_timestamp(date), sum(ksi2k) FROM accounting where lhc_vo like 'ops' group by year(date), month(date)");
$rows = array();
$rows['type'] = 'area';
$rows['name'] = 'Ops';
while($r = mysql_fetch_array($result)) {
    $rows['data'][] = $r[0]*1000;
    $rows['data'][] = $r[1];

    array_push($rows);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

The JSON results look like this:

{"type":"area","name":"Ops","data":[1167664515000,0,1170342915000,0,1172762115000,0,1175436915000,0,1178028915000,0]}

But I need the JSON results should look like this:

{"type":"area","name":"Ops","data":[[1167664515000,0],[1170342915000,0],[1172762115000,0],[1175436915000,0],[1178028915000,0]]}
1
  • This question is similar to: Php pushing values to a 2-dimensional array. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 8, 2024 at 7:24

2 Answers 2

0
while($r = mysql_fetch_array($result)) {
    $rows['data'][] = array($r[0]*1000, $r[1]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am appalled by speed, simplicity and elegance of your solutions! Everything works perfectly
I can only add that the query should be more than one. If the request only one, you have manually close the array.
@КимКлименко: I'm not sure what you mean by that, but perhaps you simply want to define $rows['data'] = array(); before the loop?
Yes, that's right $result = array();array_push($result,$rows);array_push($result,$rows1);
@КимКлименко: You shouldn't need to do any of that array_push() stuff at all.
0

In addition to my other answer, you ought to consider switching away from the ancient (and now deprecated, as of PHP v5.5) ext/mysql. Here is an example using PDO, in which you can see how simple your problem becomes:

<?php
  $con = new PDO(
    'mysql:hostname=localhost;dbname=admin_accounting',
    'user',
    'password'
  );

  $result = $con->query('
    SELECT   1000*UNIX_TIMESTAMP(date), SUM(ksi2k)
    FROM     accounting
    WHERE    lhc_vo LIKE "ops"
    GROUP BY YEAR(date), MONTH(date)
  ');

  print json_encode([
    'type' => 'area',
    'name' => 'Ops',
    'data' => $result->fetchAll(PDO::FETCH_NUM)
  ]);
?>

Note that:

  • I have moved the multiplication into the database layer in order that I can simply call fetchAll() to obtain the resulting array;

  • MySQL will select an indeterminate value from amongst those in each group for the first column in the resultset; should this be undesirable, you will need to apply a suitable aggregate function to the reference to the date column; and

  • I have used the short array syntax, which is only available from PHP v5.4—if you're using an earlier version, you will need to replace the [ … ] of the argument to json_encode() with array( … ).

1 Comment

Thank you once again for a qualified full answer.

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.