mantıken dizide aynı anahtar isimli iki eleman bulundurulamaz diye biliyorum? sizin yapınızdansa bu JSON formatı daha uygun olacaktır.
<?php
header("Content-Type: text/plain; charset=UTF-8");
$results = (object) array(
"status" => true,
"category" => (object) array(
"ID" => 1,
"title" => "title",
"description" => "description"
),
"posts" => array(
(object) array(
"ID" => 1,
"type" => "post",
"title" => "title",
"content" => "content",
"excerpt" => "excerpt",
"published_at" => (time() - (72 * 60 * 60)),
"comments" => array()
),
(object) array(
"ID" => 2,
"type" => "post",
"title" => "title",
"content" => "content",
"excerpt" => "excerpt",
"published_at" => (time() - (24 * 60 * 60)),
"comments" => array()
)
)
);
print_r(
json_encode(
$results,
JSON_PRETTY_PRINT
)
);{
"status": true,
"category": {
"ID": 1,
"title": "title",
"description": "description"
},
"posts": [
{
"ID": 1,
"type": "post",
"title": "title",
"content": "content",
"excerpt": "excerpt",
"published_at": 1384024870,
"comments": [
]
},
{
"ID": 2,
"type": "post",
"title": "title",
"content": "content",
"excerpt": "excerpt",
"published_at": 1384197670,
"comments": [
]
}
]
}Çıktıyı t6.json dosyasına kaydettikten sonra dosyadan okuyup örnek işlemler yapabiliriz.
<?php
header("Content-Type: text/plain; charset=UTF-8");
$response = file_get_contents("t6.json");
$results = json_decode($response);
print_r($results);
foreach($results->posts as $post) {
echo "\x20\n";
print_r($post);
echo "\x20\n";
}Örnek kullanımın çıktısı şu şekilde olacaktır.
stdClass Object
(
[status] => 1
[category] => stdClass Object
(
[ID] => 1
[title] => title
[description] => description
)
[posts] => Array
(
[0] => stdClass Object
(
[ID] => 1
[type] => post
[title] => title
[content] => content
[excerpt] => excerpt
[published_at] => 1384025003
[comments] => Array
(
)
)
[1] => stdClass Object
(
[ID] => 2
[type] => post
[title] => title
[content] => content
[excerpt] => excerpt
[published_at] => 1384197803
[comments] => Array
(
)
)
)
)
stdClass Object
(
[ID] => 1
[type] => post
[title] => title
[content] => content
[excerpt] => excerpt
[published_at] => 1384025003
[comments] => Array
(
)
)
stdClass Object
(
[ID] => 2
[type] => post
[title] => title
[content] => content
[excerpt] => excerpt
[published_at] => 1384197803
[comments] => Array
(
)
)