将php字符串转换为数组

时间:2016-06-08 11:35:25

标签: java php arrays

我使用PHP(通过ajax)检索数据并在java中使用它。我打算使用谷歌图表的信息。示例如下:

(1)java中的PHP输出/输入

[[8058, 26.4]/[8081, 26.5]/[8091, 26.5]/[8092, 26.5]/[8094, 26.5]/[8096, 26.5]/[8104, 26.6]/[8109, 26.6]/[8120, 26.6]/] 

(2)我将上面的PHP转换为字符串数组 - 如下所示:

[8058, 26.4],[8081, 26.5],[8091, 26.5],[8092, 26.5],[8094, 26.5],[8096, 26.5],[8104, 26.6],[8109, 26.6],[8120, 26.6] 

(3)但是必须将其转换为可由Google Charts读取的数组。我可以在Google Charts中成功阅读以下数组

p2 = ([[5, 23.8],[6, 24.0],[7, 24.1],[8, 24.2],[9, 24.3]]);

那么如何将(2)转换为Google Charts可以读取的数组呢?我不是一个Java专家,而且有点困在这里......欢迎任何建议。

2 个答案:

答案 0 :(得分:0)

[8058, 26.4],[8081, 26.5],[8091, 26.5],[8092, 26.5],[8094, 26.5],[8096, 26.5],[8104, 26.6],[8109, 26.6],[8120, 26.6] 
$data = [8058, 26.4]@[8081, 26.5]@[8091, 26.5]@[8092, 26.5]@[8094, 26.5]@[8096, 26.5]@[8104, 26.6]@[8109, 26.6]@[8120, 26.6]
This string change comma separated values by other character like @ after that execute below function.
print_r(explode ( '@' , $data));
**OUTPUT:**
array (
  0 => '[8058, 26.4]',
 1 => '[8081, 26.5]',
 2 => '[8091, 26.5]',
 3 => '[8092, 26.5]',
 4 => '[8094, 26.5]',
 5 => '[8096, 26.5]',
 6 => '[8104, 26.6]',
 7 => '[8109, 26.6]',
 8 => '[8120, 26.6]',
)

答案 1 :(得分:0)

感谢您的反馈和建议/不确定如何将上述内容集成到我的代码中(爆炸不起作用)。

我也在看JSON /但是首先想把它从我的盘子里拿出来。

我的下面的行:

<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={
    'modules':[{
        'name':'visualization',
        'version':'1.1',
        'packages':['corechart']
    }]
}"></script>
</head>

<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.setOnLoadCallback(drawChart);

    function drawChart() {

        var data = new google.visualization.DataTable();
        data.addColumn('number', 'X');
        data.addColumn('number', 'temp');

        p2 = ([[5, 23.8],[6, 24.0],[7, 24.1],[8, 24.2],[9, 24.3]]); //Test array to check Google Line Chart -> works

        // Retrieve data via  php       
        var phpData = $.ajax({
            url: "test.php",
            dataType: "text",
            async: false
        }).responseText;

        var ary = phpData.replace('[[','[').replace('/]','').split('/');

        data.addRows(p2);       // This works to draw line chart (test array P2)

        var options = {
            title: 'Any',
            curveType: 'function',
            legend: { position: 'bottom' },
            hAxis: { title: 'Time', logScale: false },
            vAxis: { title: 'T', logScale: false },
            colors: ['#a52714', '#097138']
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
        chart.draw(data, options);
    }
</script>
    <div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>