0

I'm trying to retrieve values from a database and encode them as JSON for another script to grab. I'm doing this like this:

for($i=0; $i<7; $i++)
			{
				$nextDay = $i+1;
			  	$startDateTime = date("Y-m-d H:i:s", strtotime("-$i days"));
		  		$endDateTime = date("Y-m-d H:i:s", strtotime("-$nextDay days"));
		  		$query = "select count(*) as cnt from app_instances where (time between '$endDateTime' and '$startDateTime') and (app_id = '$appId')  group by title";
		  		$result = $database->prepare($query);
				$result->execute();
				$row = $result->fetch();
				$count = (int)$row['cnt'];
				if($count==null){
					$count = 0;
				}
				$thisVal = array();
				$thisVal[]= $count;
				array_push($dataArray,$thisVal);	

			}

			$thisContent = json_encode($dataArray);

This gives the result:

[[30],[152],[45],[34],[56],[67],[56]]

But what I really need is something like:

[[1, 30], [2, 152], [3, 45], [4, 34], [5, 56], [6,67], [7, 56]]

But if I try to add the key to the array manually, it returns as objects.
Can anyone help?

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by network18
0

just note that json_encode() returns the data in

{"a":1,"b":2,"c":3}

this fashion if the array passed to it is -

array('a'=>1,'b'=>2,'c'=>3);

otherwise have a look at this customized thing

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.