2

So I have a php array like this:

$myArray = array(
'name' => 'Series Name',
'data' => array(
    array("2012-11-13",0.71),
    array("2012-11-14",0.45),
    array("2012-11-15",0.65),
    array("2012-11-16",0.67),
    )
);

I would like to output this array in json_encoded form with the array keys as object names(?). When I echo json_encode the array I get this:

{
    "name": "Series Name",
    "data": [
        [
            "2012-11-13",
            0.71
        ],
        [
            "2012-11-14",
            0.45
        ],
        [
            "2012-11-15",
            0.65
        ],
        [
            "2012-11-16",
            0.67
        ]
    ]
}

The trouble with this is that the array keys ("name" and "data") are output as strings. I need them to be objects. I.e. without the quotes around them.

EDIT: The purpose is this is to build and use a php array with highcharts. As can be seen in the source of this chart the data is passed in that way:

$(function () {
        $('#container').highcharts({
            chart: {
                type: 'spline'
            },
            title: {
                text: 'Snow depth in the Vikjafjellet mountain, Norway'
            },
            subtitle: {
                text: 'An example of irregular time data in Highcharts JS'
            },
            xAxis: {
                type: 'datetime',
                dateTimeLabelFormats: { // don't display the dummy year
                    month: '%e. %b',
                    year: '%b'
                }
            },
            yAxis: {
                title: {
                    text: 'Snow depth (m)'
                },
                min: 0
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%e. %b', this.x) +': '+ this.y +' m';
                }
            },

            series: [{
                name: 'Winter 2007-2008',
                // Define the data points. All series have a dummy year
                // of 1970/71 in order to be compared on the same x axis. Note
                // that in JavaScript, months start at 0 for January, 1 for February etc.
                data: [
                    [Date.UTC(1970,  9, 27), 0   ],
                    [Date.UTC(1970, 10, 10), 0.6 ],
                    [Date.UTC(1970, 10, 18), 0.7 ],
                    [Date.UTC(1970, 11,  2), 0.8 ],
                    [Date.UTC(1970, 11,  9), 0.6 ],
                    [Date.UTC(1970, 11, 16), 0.6 ],
                    [Date.UTC(1970, 11, 28), 0.67],
                    [Date.UTC(1971,  0,  1), 0.81],
                    [Date.UTC(1971,  0,  8), 0.78],
                    [Date.UTC(1971,  0, 12), 0.98],
                    [Date.UTC(1971,  0, 27), 1.84],
                    [Date.UTC(1971,  1, 10), 1.80],
                    [Date.UTC(1971,  1, 18), 1.80],
                    [Date.UTC(1971,  1, 24), 1.92],
                    [Date.UTC(1971,  2,  4), 2.49],
                    [Date.UTC(1971,  2, 11), 2.79],
                    [Date.UTC(1971,  2, 15), 2.73],
                    [Date.UTC(1971,  2, 25), 2.61],
                    [Date.UTC(1971,  3,  2), 2.76],
                    [Date.UTC(1971,  3,  6), 2.82],
                    [Date.UTC(1971,  3, 13), 2.8 ],
                    [Date.UTC(1971,  4,  3), 2.1 ],
                    [Date.UTC(1971,  4, 26), 1.1 ],
                    [Date.UTC(1971,  5,  9), 0.25],
                    [Date.UTC(1971,  5, 12), 0   ]
                ]
            }, {
                name: 'Winter 2008-2009',
                data: [
                    [Date.UTC(1970,  9, 18), 0   ],
                    [Date.UTC(1970,  9, 26), 0.2 ],
                    [Date.UTC(1970, 11,  1), 0.47],
                    [Date.UTC(1970, 11, 11), 0.55],
                    [Date.UTC(1970, 11, 25), 1.38],
                    [Date.UTC(1971,  0,  8), 1.38],
                    [Date.UTC(1971,  0, 15), 1.38],
                    [Date.UTC(1971,  1,  1), 1.38],
                    [Date.UTC(1971,  1,  8), 1.48],
                    [Date.UTC(1971,  1, 21), 1.5 ],
                    [Date.UTC(1971,  2, 12), 1.89],
                    [Date.UTC(1971,  2, 25), 2.0 ],
                    [Date.UTC(1971,  3,  4), 1.94],
                    [Date.UTC(1971,  3,  9), 1.91],
                    [Date.UTC(1971,  3, 13), 1.75],
                    [Date.UTC(1971,  3, 19), 1.6 ],
                    [Date.UTC(1971,  4, 25), 0.6 ],
                    [Date.UTC(1971,  4, 31), 0.35],
                    [Date.UTC(1971,  5,  7), 0   ]
                ]
            }, {
                name: 'Winter 2009-2010',
                data: [
                    [Date.UTC(1970,  9,  9), 0   ],
                    [Date.UTC(1970,  9, 14), 0.15],
                    [Date.UTC(1970, 10, 28), 0.35],
                    [Date.UTC(1970, 11, 12), 0.46],
                    [Date.UTC(1971,  0,  1), 0.59],
                    [Date.UTC(1971,  0, 24), 0.58],
                    [Date.UTC(1971,  1,  1), 0.62],
                    [Date.UTC(1971,  1,  7), 0.65],
                    [Date.UTC(1971,  1, 23), 0.77],
                    [Date.UTC(1971,  2,  8), 0.77],
                    [Date.UTC(1971,  2, 14), 0.79],
                    [Date.UTC(1971,  2, 24), 0.86],
                    [Date.UTC(1971,  3,  4), 0.8 ],
                    [Date.UTC(1971,  3, 18), 0.94],
                    [Date.UTC(1971,  3, 24), 0.9 ],
                    [Date.UTC(1971,  4, 16), 0.39],
                    [Date.UTC(1971,  4, 21), 0   ]
                ]
            }]
        });
    });

Is this json or another way of forming javascript objects?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
harryg
  • 23,311
  • 45
  • 125
  • 198
  • 2
    Then it will create invalid `JSON`. – Yogesh Suthar May 08 '13 at 11:01
  • So what is it that the keys are suddenly objects if you do not quote them? Also in your example array, they are key values in string .. how do you magically plan to convert it into an object? – dbf May 08 '13 at 11:19
  • I don't know if I have used the correct wording. My aim is to form code in the same structure as required by highcharts, which I think is a js object for a series of a chart. – harryg May 08 '13 at 11:22
  • Can you post the "structure" - it would be easier to try and work out what you mean by looking at the segment of code you are trying to apply the JSON object in. – Emissary May 08 '13 at 11:28
  • If your axes are correct then this could be processed by HCs indeed, but what does this have to do with quotes around key value names? – dbf May 08 '13 at 11:31
  • @Emissary it is in my original question, in the edit – harryg May 08 '13 at 11:47
  • @dbf Oh OK. It's just the HC example doesn't have quotes around the keys so I assumed having quotes would not work – harryg May 08 '13 at 11:49
  • Sorry I missed that - the only difference I can see in the HC example is that the "series" key is an array of objects - in which case you could use: `'series': [].push(myObj)` – Emissary May 08 '13 at 11:52
  • The quotes are necessary to be a valid json string. But, they are not a problem to get the correct object in javascript. If you write quotes or not, the object will be the same. Can you paste your template, where you add your json string into the javascript? I can imagine, that your misses and additional array around $myArray. – micha149 May 08 '13 at 12:23
  • Ok thanks. I actually didn't even try with having quotes around the object keys. Having now tried it it does indeed work fine - so my problem was not a problem to begin with. Thanks for the help. – harryg May 08 '13 at 13:25
  • 1
    The Highcharts example you've linked does not use JSON at all. It's JavaScript code, possibly using the jQuery library. JSON is not JavaScript, it's not even a programming language. – Álvaro González May 08 '13 at 14:22

2 Answers2

1

Assuming you want to use JSON object in client side, for browsers support JSON.parse(); you can parse your JSON string by using JavaScript:

obj = JSON.parse(json);

Which json is your JSON string passed from server (result of json_encode($array); in your PHP file), However for browser compatibility, you can use jQuery:

obj = $.parseJSON(json);
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
-1

Use stdClass.

$obj = new stdClass();

$obj->name = "Series Name";
$obj->data = array(
    array("2012-11-13",0.71),
    array("2012-11-14",0.45),
    array("2012-11-15",0.65),
    array("2012-11-16",0.67),
    );

echo json_encode($obj);

If you don't know what stdClass is, please, look at this topic

EDIT: In order for JSON to produce valid code, all the key/value pairs, except for numeric ones, must be inside quotes

Community
  • 1
  • 1
João Dias
  • 1,078
  • 1
  • 15
  • 38
  • Unfortunatey that doesn't seem to work. The keys are still in quotes. – harryg May 08 '13 at 11:08
  • 2
    @harryg Of course they are! And they need to be to produce a valid JSON object. Only numeric items can appear without quotation. http://www.json.org/ – João Dias May 08 '13 at 11:11
  • Associative array and objects will result in the same json syntax – micha149 May 08 '13 at 12:18
  • @micha149 I know that, however, objects, in my opinion, produce a prettier and far more understandable code for `json_encode` to use. Nevertheless, I answered the question in the comments – João Dias May 08 '13 at 14:08