10

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?

1
  • 1
    Why don't you Merge the 2 arrays and then Encode them as JSON? Commented Mar 28, 2013 at 7:59

4 Answers 4

12

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);
Sign up to request clarification or add additional context in comments.

Comments

4

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": {
        ........
    }
 }
]

Comments

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.

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.

Comments

1

You need to combine both array using array_merge().

Example

$response = array();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.