Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get two arrays from php using json,jquery ajax?

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

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

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

So any one has idea of how to do it?

like image 587
nikhil Avatar asked Mar 28 '13 07:03

nikhil


4 Answers

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

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

and get data by

initChart2(data.result1,data.result2);
like image 182
bipen Avatar answered Nov 06 '22 19:11

bipen


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

    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": {
        ........
    }
 }
]
like image 4
Jai Avatar answered Nov 06 '22 18:11

Jai


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.

So basically, your php code will be:

<?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.

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

$.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.

like image 2
ksg91 Avatar answered Nov 06 '22 19:11

ksg91


You need to combine both array using array_merge().

Example

$response = array();

$response = array_merge($arr1,$arr2);
echo json_encode($response);
like image 1
Dipesh Parmar Avatar answered Nov 06 '22 19:11

Dipesh Parmar