javascript - Convert a 2D PHP array to a 2D Json object to plot on a graph -


i'm working on part of code since weeks , i'm unable solve issue. i'll try explain code deeply.

i'm fetching values mysql database. using code below i'm able create 2d php array:

        ($i=2, $k=2; $i<11, $k<11; $i++, $k++) {             $roe_array[] = array( $date_y[$i] => $roe[$k]);         } 

the output of code following

array (     [0] => array         (             [2006] => 13.83         )      [1] => array         (             [2007] => 16.43         )      [2] => array         (             [2008] => 14.89         )      [3] => array         (             [2009] => 18.92         )      [4] => array         (             [2010] => 22.84         )      [5] => array         (             [2011] => 27.06         )      [6] => array         (             [2012] => 28.54         )      [7] => array         (             [2013] => 19.34         )      [8] => array         (             [2014] => 18.01         )  ) 

so, convert 2d php array in json

$roe_enc = json_encode( $roe_array ); 

the output is:

[{"2006":"13.83"},{"2007":"16.43"},{"2008":"14.89"},{"2009":"18.92"},{"2010":"22.84"},{"2011":"27.06"},{"2012":"28.54"},{"2013":"19.34"},{"2014":"18.01"}] 

considering have plot in javascript script, want object following:

[[2006, 13.83],[2007, 16.43],[2008, 14.89],[2009, 18.92], [2010, 22.84],[2011, 27.06],[2012, 28.54],[2013, 19.34],[2014, 18.01]] 

can me solve issue? support.

now year key , want value in array have generate array contains 0-based indexed arrays instead of key-value pairs. reason other index sequential 0-based ones converted key-value pairs in json javascript knows 0-based arrays.

to should change loop to:

for ($i=2, $k=2; $i<11, $k<11; $i++, $k++) {     // add array 2 values instead of key-value pair     $roe_array[] = array( $date_y[$i], $roe[$k] );     //                               ^ add 2 elements instead of 1 } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -