Jump to content

MySQL to Multi Dimensional Array Help


jontilt

Recommended Posts

Hi there,

 

I have written an array of data so that it json_encodes into the correct format required for a charting component that I am using. The php array looks like this:

 

First Number = hours

Second Number = data

 

$menu['metric1'] = array(

 

label => "Metric 1",

data => array(

 

array ('00',48),

array ('01',47),

array ('02',45),

array ('03',40),

array ('04',42),

array ('05',48),

array ('06',38),

array ('07',28),

array ('08',40),

array ('09',30),

array ('10',38),

array ('11',28),

array ('12',18),

 

)

         

    );

   

    $menu['metric2'] = array(

 

label => "Metric 2",

data => array(

 

array ('00',30),

array ('01',44),

array ('02',35),

array ('03',49),

array ('04',32),

array ('05',47),

array ('06',38),

array ('07',23),

array ('08',30),

array ('09',34),

array ('10',38),

array ('11',20),

array ('12',31),

 

)

         

    );

 

When I json encode this, it is represented as this which is perfect.

 

{

"metric1": {

"label":"Metric 1",

"data":[["00",48],["01",47],["02",45],["03",40],["04",42],["05",48],["06",38],["07",28],["08",40],["09",30],["10",38],["11",28],["12",18]]

},

 

"metric2": {

"label":"Metric 2",

"data":[["00",30],["01",44],["02",35],["03",49],["04",32],["05",47],["06",38],["07",23],["08",30],["09",34],["10",38],["11",20],["12",31]]

}

 

}

 

What I am trying to do now is to create that php array from a database, so I have loaded the data into my DB, I have run the correct select statement to extract my data . . . . . and this is where I am now stuck.

 

How would I code up my db result set to form my multidimensional array?????

 

Any help would be greatly appreciated . . . .going a little mad trying to figure this out.

 

Cheers

 

Jon

 

 

Link to comment
https://forums.phpfreaks.com/topic/205322-mysql-to-multi-dimensional-array-help/
Share on other sites

Hi all,

 

I have managed to solve it, so thought I would post the answer just incase anyone else had the same problem.

 

function select_metrics($start_date, $end_date, $location_id) {

 

$query = "SELECT * FROM xxx WHERE Date BETWEEN '$start_date' AND '$end_date' AND xxx = '$location_id'";

 

$result = mysql_query($query) or die(mysql_error());

 

 

$data = array();

$master = array();

 

$data_two = array();

$master_two = array();

 

while( $row=mysql_fetch_assoc($result) ) {

 

$time = explode(':', $row['Time']);

 

$data = array($time[0], $row['LAeq']);

array_push($master, $data);

 

$data_two = array($time[0], $row['LAE']);

array_push($master_two, $data_two);

 

}

 

$menu['metric1'] = array(

 

label => "Metric 1",

data => $master,

 

         

    );

 

$menu['metric2'] = array(

 

label => "Metric 2",

data => $master_two,

 

         

    );

 

  echo json_encode($menu);

 

}

 

Cheers

 

Jon

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.