1

I am confused writing this one, how can i achieve achieve this similar output. I'm doing json encode since early. But this one is different for me.

 [{
        type: 'pie',
        name: 'Browser share',
        innerSize: '40%',
        data: [
            ['Firefox',   10.38],
            ['IE',       56.33],
            ['Chrome', 24.03],
            ['Safari',    4.77],
            ['Opera',     0.91],
            {//over here i can't get this braces
                name: 'Proprietary or Undetectable',
                y: 0.2,
                dataLabels: {
                    enabled: false
                }
            }
        ]
    }]

Here is my code,

$percentage = array();
$arr2 = new stdClass();
$arr2->type = 'pie';
$arr2->name = 'Browser share';
$arr2->innerSize = '40%';
while($azSoc = mysql_fetch_assoc($az)){ 
    $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']);
}
$arr2->name = 'Porperty ok';
$arr2->y = 0.2;
$arr2->dataLabels[] = array('enable',false);
array_push($percentage, $arr2);
echo json_encode( $percentage);

The output of my codes gives me,

[{"type":"pie","name":"Porperty ok","innerSize":"40%","data":[["tes","25"],
["awdawdas","25"],["awdawd","25"],["ehhehe","25"]],"y":0.2,"dataLabels":
[["enable",false]]}]
2
  • What you are trying to achieve is not valid JSON. Commented Sep 18, 2016 at 11:23
  • But how, maybe I am wrong with my question is it an array? Because I already did it on my other code several times. Putting in js and rewrite by json.parse works for me Commented Sep 18, 2016 at 11:25

2 Answers 2

2

What you are trying to achieve is not valid json. You need to create a new occurance in your array with a name some_name and then load the object into that occurance

$percentage = array();

$arr2 = new stdClass();
$arr2->type = 'pie';
$arr2->name = 'Browser share';
$arr2->innerSize = '40%';
while($azSoc = mysql_fetch_assoc($az)){ 
    $arr2->data[] = array("".$azSoc['criteria_name']."", $azSoc['crit_score_percentage']);
}

$t = new stdClass();
$t->name = 'Porperty ok';
$t->y = 0.2;
$t->dataLabels = array('enable',false);
$arr2->data['some_name'] = $t;

$percentage[] = $arr2;

//dont know where $cat came from I assume its a larger oject you are building
echo json_encode( $cat);   
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of array('enable',false); It should be array('enable' => false);

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.