如何使用json、jquery ajax从php获取两个数组?

时间:2022-11-26 12:15:42

I want to build a graph in jqchart where i need to get two arrays

我想在jqchart中构建一个图,其中我需要得到两个数组

Now i want to perform operation as given below.Which is giving error ofcourse.

现在我要执行如下所示的操作。这当然是错误的。

html
 $.ajax(
        {
            type: "GET",
            url: "customer_coverage.php",
            data: {id:id},
            contentType: "application/json",
            dataType: "json",
            success: function (data21,data22) {

                initChart2(data21,data22);
            }
        });




        function initChart2(data21,data22) {
            $('#jqChart2').jqChart({


                series: [
                {
                            type: 'column',
                            title: 'no of days ',
                data:data21,

                        },
                {
                            type: 'column',
                            title: 'no of days ',
            data:data22,

                        },


                        ]
            });
        }

heres PHP code

这是PHP代码

  echo json_encode($arr1);
  echo json_encode($arr2);

So any one has idea of how to do it?

有人知道怎么做吗?

4 个解决方案

#1


3  

See if you are able to produce two object arrays of json then you can try with this:

看看您是否能够生成两个json对象数组,然后您可以尝试以下操作:

    var data21,data22; 
    $.ajax({
        type: "GET",
        url: "customer_coverage.php",
        data: {id:id},
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $.each(data, function(i, item){
               data21 = item.data21;
               data22 = item.data22;
            });
            initChart2(data21,data22);
        }
    });

and i am supposing if you are able to produce this:

我假设如果你能生产这个

[
 {
    "data21": {
        .........
    },
    "data22": {
        ........
    }
 }
]

#2


10  

no need to echo json encode two times....merge the array and send the data.......

不需要呼应json编码....两倍合并数组并将数据发送给……

echo json_encode(array('result1'=>$arr1,'result2'=>$arr2));

and get data by

和获取数据

initChart2(data.result1,data.result2);

#3


2  

You cannot get multiple object like that. For a JSON object, you will need to have single object. So what you can do is, create a wrapper object put those two array inside it.

你不可能得到这样的多个对象。对于JSON对象,您将需要一个对象。你可以做的是,创建一个包装器对象把这两个数组放在里面。

So basically, your php code will be:

基本上,你的php代码是:

<?php
$arr= array();
$arr['arr1'] = $arr1;
$arr['arr2'] = $arr2;

echo json_encode($arr);
?>

So now you will have single main array and so single JSON object.

现在你将有单个主数组和单个JSON对象。

On JS side, you will get single data. Little Modification will be

在JS端,您将获得单个数据。小的修改将

$.ajax(
        {
            type: "GET",
            url: "customer_coverage.php",
            data: {id:id},
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
            var data21=data['arr1'];
            var data22=data['arr2'];
                initChart2(data21,data22);
            }
        });

This should work.

这应该工作。

#4


1  

You need to combine both array using array_merge().

您需要使用array_merge()组合这两个数组。

Example

例子

$response = array();

$response = array_merge($arr1,$arr2);
echo json_encode($response);

#1


3  

See if you are able to produce two object arrays of json then you can try with this:

看看您是否能够生成两个json对象数组,然后您可以尝试以下操作:

    var data21,data22; 
    $.ajax({
        type: "GET",
        url: "customer_coverage.php",
        data: {id:id},
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $.each(data, function(i, item){
               data21 = item.data21;
               data22 = item.data22;
            });
            initChart2(data21,data22);
        }
    });

and i am supposing if you are able to produce this:

我假设如果你能生产这个

[
 {
    "data21": {
        .........
    },
    "data22": {
        ........
    }
 }
]

#2


10  

no need to echo json encode two times....merge the array and send the data.......

不需要呼应json编码....两倍合并数组并将数据发送给……

echo json_encode(array('result1'=>$arr1,'result2'=>$arr2));

and get data by

和获取数据

initChart2(data.result1,data.result2);

#3


2  

You cannot get multiple object like that. For a JSON object, you will need to have single object. So what you can do is, create a wrapper object put those two array inside it.

你不可能得到这样的多个对象。对于JSON对象,您将需要一个对象。你可以做的是,创建一个包装器对象把这两个数组放在里面。

So basically, your php code will be:

基本上,你的php代码是:

<?php
$arr= array();
$arr['arr1'] = $arr1;
$arr['arr2'] = $arr2;

echo json_encode($arr);
?>

So now you will have single main array and so single JSON object.

现在你将有单个主数组和单个JSON对象。

On JS side, you will get single data. Little Modification will be

在JS端,您将获得单个数据。小的修改将

$.ajax(
        {
            type: "GET",
            url: "customer_coverage.php",
            data: {id:id},
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
            var data21=data['arr1'];
            var data22=data['arr2'];
                initChart2(data21,data22);
            }
        });

This should work.

这应该工作。

#4


1  

You need to combine both array using array_merge().

您需要使用array_merge()组合这两个数组。

Example

例子

$response = array();

$response = array_merge($arr1,$arr2);
echo json_encode($response);