У меня есть массив JSON в PHP, что я хочу сделать, это передать это как два массива в сценарий Java,
[{"mon":"January","total":"29674"},{"mon":"February","total":"22956"},{"mon":"March","total":"35893"},{"mon":"April","total":"64003"},{"mon":"May","total":"43047"},{"mon":"June","total":"44613"},{"mon":"July","total":"72003"},{"mon":"August","total":"116742"}]
здесь массивы должны выглядеть так,
var s1 = [January, February, March, April , ....];
var s2 = [29674, 22956, 35893, 64003, .....];
Извините, на данный момент я не знаю, как использовать jsfiddle. Вот решение:
var b = [{"mon":"January","total":"29674"},{"mon":"February","total":"22956"},{"mon":"March","total":"35893"},{"mon":"April","total":"64003"},{"mon":"May","total":"43047"},{"mon":"June","total":"44613"},{"mon":"July","total":"72003"},{"mon":"August","total":"116742"}]
var x = [], y = [];
for (i = 0; i < b.length; i++){ x.push(b[i].mon); y.push(b[i].total) }
результат:
["January", "February", "March", "April", "May", "June", "July", "August"]
["29674", "22956", "35893", "64003", "43047", "44613", "72003", "116742"]
Итак, сначала вы должны перебрать свой первый массив и сохранить его в отдельных массивах.
<?php
$input = '[{"mon":"January","total":"29674"},{"mon":"February","total":"22956"},{"mon":"March","total":"35893"},{"mon":"April","total":"64003"},{"mon":"May","total":"43047"},{"mon":"June","total":"44613"},{"mon":"July","total":"72003"},{"mon":"August","total":"116742"}]';
$inputArray = json_decode($input, true);
$s1 = array();
$s2 = array();
foreach ($inputArray as $row) {
$s1[] = $row['mon'];
$s2[] = (int) $row['total'];
}
?>
Затем вы должны распечатать их в своем скрипте:
<script>
var s1 = <?php echo json_encode($s1); ?>;
var s2 = <?php echo json_encode($s2); ?>;
</script>