简体   繁体   中英

json_encode with a multidimensional array

I've been looking at json_encode tips for a bit, and haven't been able to figure out yet what I'm trying to do.

I've built a php function that takes a CSV file and dumps the data into a multidimensional array (I am 99% sure this is correct, at least). I need to then take the data from the array, and put it into a line graph. Here's my PHP code that retrieves the data from the CSV (and adds a value at the end that is a calculation of 2 of the other values within the array):

<?php

function readCSV($csvFile) {
    $file_handle=fopen($csvFile,'r');
    while (!feof($file_handle) ) {
        $line_of_text[]=fgetcsv($file_handle,1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

// Set path to CSV file
$csvFile='./example.csv';

$csv=readCSV($csvFile);
array_push($csv[0], "Percentage");
$headers = array_shift($csv);
$i=0;
foreach ($csv as $record) {
    $Perc = $record[2] / $record[3];
    $Perc = $Perc * 100;
    $Perc = round ($Perc, 2);
    array_push($csv[$i], $Perc);
    $j=0;
    $i++;
}
foreach ($csv as $key => $row) {
    $year[$key] = $row[0];
}
array_multisort($year, SORT_ASC, $csv);

//print_r($csv);
echo json_encode($csv);

?>

That echo gives me an array with the following results:

[["2008","41","412","525","125.2",78.48],["2009","33","393","571","99.9",68.83],["2010","33","450","679","91.9",66.27],["2012","37","400","583","105.8",68.61],["2013","55","450","659","115.1",68.29]]

The first key is the year. In my graph, I need to chart based on the year on the x-axis, and I need to chart key 1, 5, and 6 (all based on year). I have not been able to figure out how to do this.

The format for the graph is something like this:

var d1 = [[0, 3], [4, 8], [8, 5], [12, 13]];

var d1 = [[2009, 33], [2010, 33], [2012, 37], [2013, 55]];
var d2 = [[2009, 99.9], [2010, 91.9], [2012, 105.8], [2013, 115.1]];
var d3 = [[2009, 68.83], [2010, 66.27], [2012, 68.61], [2013, 68.29]];

where the first value in each set is the x-axis, the 2nd value is the y-axis.

any help would be MUCH appreciated. Thanks!

Before encoding the JSON, you have already successfully sorted your array $year with array_multisort() . The next thing to do is to iterate it 3 times, one each for your three output variables and produce the 2D array for each one. Then after you have those, you can use PHP to write out the JavaScript and call json_encode() on the individual arrays.

// You already sorted $year how you need it
array_multisort($year, SORT_ASC, $csv);

// This will store your final output
$mapped_values = array();

// You want the keys 1,4,5
foreach (array(1,4,5) as $key) {
  $temp = array();
  // Loop over each year array and make a sub-array of year,key
  // 2008, 2008, 2010....
  foreach ($csv as $y) {
    // intval() & floatval() prevent the output as strings to JSON...
    $temp[] = array(intval($y[0]), floatval($y[$key]);
  }
  // Append the structure to the outer result
  // Each of these corresponds to one of the keys 1,4,5...
  $mapped_values[] = $temp;
}

// Write it as output:
foreach ($mapped_values as $varnum => $js_var) {
  // To get the d1, d2, d3
  $num = $varnum + 1;
  // Write a var dN = JSON...
  // json_encode() will produce the appropriate output
  echo "var d{$num} = " . json_encode($js_var) . ";\n";
}

This produces the following output:

var d1 = [[2008,41],[2009,33],[2010,33],[2012,37],[2013,55]];
var d2 = [[2008,125.2],[2009,99.9],[2010,91.9],[2012,105.8],[2013,115.1]];
var d3 = [[2008,78.48],[2009,68.83],[2010,66.27],[2012,68.61],[2013,68.29]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM