LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2012, 01:30 PM   #1
Tyler_H72
Member
 
Registered: May 2008
Distribution: OpenSuSE
Posts: 65

Rep: Reputation: 15
PHP Array Indices Converting As Keys


I am working with some PHP code right now, trying to return JSON to calling pages. The problem I am running into is that one of the arrays I am using is returning the array indexes as key values. The code I am using is roughly as follows:
Code:
	$data = pg_query($query) or die('Data query failed: ' . pg_last_error());
	$curDate = new DateTime('1970-01-01 00:00:00');
	$dataPoint = 0;
	while($row = pg_fetch_row($data)) {
		$newDate = new DateTime($row[0] . " -" . $offset . ' hours');
		if($curDate != $newDate) {
			$dataPoint++;
			$curDate = $newDate;
			$output['catalog'][$dataPoint] = array();
			$output['catalog'][$dataPoint]['dt'] = $curDate->format('Y-m-d H:i:s');
			$output['catalog'][$dataPoint]['val'] = array();
			foreach ($ids as $i => $id) {
				$output['catalog'][$dataPoint]['val'][$i] = null;
			}
		}
		
		foreach($ids as $i => $id) {
			if($id == $row[1]) {
				$output['catalog'][$dataPoint]['val'][$i] = number_format(((((intval($row[2]) * $conversion[$i][0]) + $conversion[$i][1]) * $conversion[$i][2]) + $conversion[$i][3]), $conversion[$i][4], '.', '');
				break;
			}
		}
	}

	echo json_encode($output);
(The query pulls in a very large amount of data, one row per variable sought, many of which are read at the same time, so the code is meant to loop through and combine all concurrent data points into one array, then put that array in an array of data polling times)


The problem is that the JSON I receive has the format below:
Code:
{"catalog":{"1":{"dt":"2009-07-16 20:02:33","val":[...,...,...]},"2":{"dt":"2009-07-16 20:03:17","val":[...,...,...]},"3":{"dt":"2009-07-16 20:04:17","val":[...,...,...]}
Anyone know why this is happening? I need to not show the indexes of the objects which contain 'dt' and the array 'val' (here '1', '2', and '3') in order to comply with the expected data format, but I have no idea how to do this. Thanks for any help you can offer!

Last edited by Tyler_H72; 06-21-2012 at 01:31 PM.
 
Old 06-21-2012, 01:34 PM   #2
Tyler_H72
Member
 
Registered: May 2008
Distribution: OpenSuSE
Posts: 65

Original Poster
Rep: Reputation: 15
I spoke too soon- I have solved the problem. It seems that the issue was caused by the value of $dataPoint. Because it was incremented from 0 before ever being used, the array 'catalog' did not contain the index '0'. After changing the initial value of $dataPoint to '-1', the JSON code no longer had the indices listed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting - Accessing Array Indices Jus144tice Programming 7 01-10-2015 03:39 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
Converting Image to Byte Array (Java) megabot Programming 2 03-17-2007 09:49 AM
java; converting strings to char into an array xemous Programming 5 03-25-2006 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration