Jump to content

Loop An Array


chasethemetal

Recommended Posts

Hey all! I have a question in how one could dynamically build an array. This is what my static array looks like and my dynamic one needs to be formatted like.

 $data = Array ();$data [] = Array ("Date", "Count1", "Count2");$data [] = Array ("2008", 300, 400);$data [] = Array ("2009", 500, 700);$data [] = Array ("2010", 700, 300);$data [] = Array ("2011", 900, 100);$data [] = Array ("2012", 500, 400); 

Now I have an API giving me the information I need but it is not formatted at all like this. This is the source of information that needs to be formatted like above.

 foreach($g->getResults() as $result): $date = $result;$result->getCount1();$result->getCount2(); endforeach;

This will loop out the date and count's. I'm just not sure how to put this all into the above format. Again any tips n tricks would be appreciated!!!

Link to comment
Share on other sites

Stop your typing figured it out!

 if ($stats2 != '') {$stats2.=', ';} $stats1 = '[["Date","Count1","Count2"],';$stats2 .= '["'.$date.'",'.$result->getCount1().','.$result->getCount2().']';$stats3 = ']';

I just needed the code to look like what that array looked like when outputted as json. and this little method worked!

Link to comment
Share on other sites

you can just use standard array method like push or $array[] = xxx in the loop and then when you're done just echo json_encode($array). You don't need to build it like a string. i.e.

$finalResults = array(array('Date','Count1','Count'));  foreach($g->results() as $result){  array_push($finalResults, array($date, $result->getCount1(), $result->getCount2()));}; echo  json_encode($finalResults);

edit: fixed bug in $finalResults initialization

Link to comment
Share on other sites

Thanks! I want to explore that method, however it didn't output the data right... This is what is needs to look like

[ ["Date","Count1","Count2"],["12/09/2011",8,14], ["12/08/2011",23,55] ]

And the output of thescientists method. The one strange thing that I noticed is it added \ slashes to the dates... and stripslashes() didn't seem to do anything.

["Date","Count1","Count",["12\/09\/2011",8,14],["12\/08\/2011",23,55] ]

This topic is more or less solved, but I have one more question for anyone out there. How would I add a parent label to this set of data? basically this is one set of data, but I'm going to need different sets of data in the same json output eventually. So what would putting the above in a group look like? Then I'm sure in my javascript I can call which data set in the json I'm looking for. Thanks!

Link to comment
Share on other sites

The slashes get added by json_encode to make sure it is valid JSON syntax. In that code the array should initially be defined like this though: $finalResults = array(array('Date','Count1','Count2')); You can use named array indexes to have it make a Javascript object with those names as properties of the object.

$finalResults = array(array('Date','Count1','Count2')); foreach($g->results() as $result){   $finalResults[] = array($date, $result->getCount1(), $result->getCount2());}; $output = array(  'resultSet1' => $finalResults);echo json_encode($output);

Link to comment
Share on other sites

The slashes get added by json_encode to make sure it is valid JSON syntax. In that code the array should initially be defined like this though: $finalResults = array(array('Date','Count1','Count2'));
ah. good catch.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...