In JSON, arrays [] only every have numeric keys, whereas objects {} have string properties. The inclusion of a array key in your second example forces the entire outer structure to be an object by necessity. The inner objects of both examples are made as objects because of the inclusion of string keys a,b,c,d.
If you were to use the JSON_FORCE_OBJECT option on the first example, you should get back a similar structure to the second, with the outer structure an object rather than an array. Without specifying that you want it as an object, the absence of string keys in the outer array causes PHP to assume it is to be encoded as the equivalent array structure in JSON.
$arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));
// Force the outer structure into an object rather than array
echo json_encode($arrytest , JSON_FORCE_OBJECT);
// {"0":{"a":1,"b":2},"1":{"c":3},"2":{"d":4}}
Answer from Michael Berkowski on Stack OverflowIn JSON, arrays [] only every have numeric keys, whereas objects {} have string properties. The inclusion of a array key in your second example forces the entire outer structure to be an object by necessity. The inner objects of both examples are made as objects because of the inclusion of string keys a,b,c,d.
If you were to use the JSON_FORCE_OBJECT option on the first example, you should get back a similar structure to the second, with the outer structure an object rather than an array. Without specifying that you want it as an object, the absence of string keys in the outer array causes PHP to assume it is to be encoded as the equivalent array structure in JSON.
$arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));
// Force the outer structure into an object rather than array
echo json_encode($arrytest , JSON_FORCE_OBJECT);
// {"0":{"a":1,"b":2},"1":{"c":3},"2":{"d":4}}
Arrays with continuous numerical keys are encoded as JSON arrays. That's just how it is. Why? Because it makes sense.
Since the keys can be expressed implicitly through the array encoding, there is no reason to explicitly encoded them as object keys.
See all the examples in the json_encode documentation.
How can I json_encode a nested array?
php - json_encode() on a multidimensional array - with string keys - Stack Overflow
PHP json_encode multidimensional array - Stack Overflow
PHP Multidimensional json array - PHP - SitePoint Forums | Web Development & Design Community
Try something like this:
//initialize array
$myArray = array();
//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);
//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;
//convert to json
$json = json_encode($myArray);
Here is a shorter way:
$myArray = array();
$myArray[] = array("id" => 1, "data" => 45);
$myArray[] = array("id" => 3, "data" => 54);
//convert to json
$json = json_encode($myArray);
A JSON array has no explicit indexes, it's just an ordered list. The only JSON data structure that has named keys is an object. The literal should make this quite obvious:
["foo", "bar", "baz"]
This array has no named indices and there isn't any provision to add any.
PHP conflates both lists and key-value stores into one array data type. JSON doesn't.
This is your object:
$parent=new StdClass();
$parent->ID=101;
$parent->ParentID=0;
$parent->Name='Root One';
$child1=new StdClass();
$child1->ID=1011;
$child1->ParentID=$parent->ID;
$child1->Name='Child One';
$parent->Children[]=$child1;
$child1_1=new StdClass();
$child1_1->ID=10111;
$child1_1->ParentID=$child1->ID;
$child1_1->Name='Child One One';
$child1->Children[]=$child1_1;
This is your JSON convert function:
echo json_encode($parent,JSON_PRETTY_PRINT);
and this is your object coded into JSON format:
{
"ID": 101,
"ParentID": 0,
"Name": "Root One",
"Children": [
{
"ID": 1011,
"ParentID": 101,
"Name": "Child One",
"Children": [
{
"ID": 10111,
"ParentID": 1011,
"Name": "Child One One"
}
]
}
]
}
The answer came later because I started learning PHP later. Anyway, some day, someone might find it useful.
If you can access the images key, then:
<?php
$json = <<<JSON
{
"title":"A Title Here",
"images":[
{
"coverType":"fanart",
"url":"some_random_file_here.jpg"
},
{
"coverType":"banner",
"url":"another_random_file_here.jpg"
},
{
"coverType":"poster",
"url":"yet_another_random_file_here.jpg"
}
]
}
JSON;
$json = json_decode($json);
print_r($json);
foreach ($json->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
?>
Gives:
Image Cover Type: banner
URL: another_random_file_here.jpg
UPDATE:
The JSON file you link to seems invalid, missing braces after each series. Here's the corrected JSON, and code:
<?php
$json = <<<JSON
[
{
"series": {
"title": "Brooklyn Nine-Nine",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/269586-15.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/269586-g3.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/269586-13.jpg"
}
],
"year": 2013
}
},
{
"series": {
"title": "The Middle",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/95021-16.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/95021-g14.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/95021-8.jpg"
}
],
"year": 2009
}
},
{
"series": {
"title": "New Girl",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/248682-43.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/248682-g20.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/248682-14.jpg"
}
],
"year": 2011
}
}
]
JSON;
$json = json_decode($json);
// echo '<pre>' .print_r($json, 1) .'</pre>';
foreach ($json as $item)
{
echo 'Title: ' .$item->series->title .'<br/>';
foreach ($item->series->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
}
?>
Gives:
Title: Brooklyn Nine-Nine Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/269586-g3.jpg
Title: The Middle Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/95021-g14.jpg
Title: New Girl Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/248682-g20.jpg
Suppose this is the variable jsondecoded, $json[images][1]->coverType;
Try and add:
header("Content-type: application/json; charset=utf-8");
Before you echo your JSON encoded result.
EDIT:
If the encoding doesn't work try the following:
while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
$trows[] = array_map('utf8_encode', $result);
}
I would see what PHP tells you:
Change
echo json_encode($trows);
to
$json = json_encode($trows);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
echo json_last_error_msg();
} else {
echo $json;
}