0

So far my JSON output is like this

[{"dateClose":"2012-07-06","countno":"6","cumulative_sumc":"103"}],[{"dateOpen":"2012-05-29","openNo":"73","cumulative_sum":"73"}]

Where in fact I'd like to have it like the below, just in one multidimensional array

[{"dateClose":"2012-07-06","countno":"6","cumulative_sumc":"103","dateOpen":"2012-05-29","openNo":"73","cumulative_sum":"73"}]

I've been using array_merge,array_combine against project1[] and project[] array, but no luck. My sample code is below. Any helps will be very much appreciated.Thanks.

   <?php
require_once('mysql.inc.php');
$dbc =@mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
$x="Set @csum := 0";
$q = "
select dateClose, countno, (@csum := @csum + countno) as cumulative_sumc
from dateclose
order by dateClose";
$rx = mysqli_query($dbc, $x);
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$project[] = $row;
}}
$y="Set @csum := 0";
$z = "
select dateOpen, openNo, (@csum := @csum + openNo) as cumulative_sum
from dateopen
order by dateOpen";
$rx = mysqli_query($dbc, $y);
$r = mysqli_query($dbc, $z);
if (mysqli_num_rows($r) > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
array_push($project, $row);
}}


echo json_encode($project);


?>
2
  • 2
    Your 1st JSON isn't valid JSON, and your 2nd isn't a multidimensional array. Commented Aug 1, 2012 at 15:12
  • Thanks , I've tried to check using jsonlint.com ..It looks like a a valid json for both ..hmmm..anyway is there a best way to get those expected format? Commented Aug 1, 2012 at 15:26

1 Answer 1

1

Try $project[] = $row; instead of $project1[] = $row; in your script to achieve your JSON output.

Sign up to request clarification or add additional context in comments.

11 Comments

there is an improvement where the [] symbol get removed ,but the json data appears on separate {} ,if possible would like something like below where it's embedded in {} [{"dateClose":"2012-07-06","countno":"6","cumulative_sumc":"103","dateOpen":"2012-05-29","openNo":"73","cumulative_sum":"73"}]
Ohh. you should have said before. Anyway, try this: Instead of again $project1[] = $row (in the second while), use this array_push($project, $row);. This should help!
can you post your complete code again to debug? Maybe update the post with latest code?
don't worry we're going to crack it. I've edited the code please use that one and try: pastebin.com/a50SjzP9
looks better with this output "[2012-05-31","1","1","2012-06-06","13","14","2012-06-07","31","45"] -the only remaining is to have those key value as well inside it i.e dateClose":"2012-05-31","countno":"1","cumulative_sumc":"1"...
|

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.