As others have stated the json you have supplied is not valid.
Looking at its structure it seems that the intent is to have an array of objects, each object having a "type" key and an embedded object with a key name that varies according to the value of the type key. (The varied type key (in both of the examples given the key is "article") holds a title, and an object which holds twitter and facebook counts.)
Two notes:
- from the broken json we are not sure if the embedded article
object is an object or an array of objects so we will show the
approach for both cases.
- probably the key name of the embedded
object is variable and thus we need to deduce it every time from the
type value (however in our examples it is always 'article'
Now to access that structure the following are valid when you know that the type is article (we suppose $data holds the decoded json):
$data[$i]['article']['title']
$data[$i]['article']['number']['facebook']
and if is embedded as an array of objects the above should be (to access the first object):
$data[$i]['article'][0]['title']
$data[$i]['article'][0]['number']['facebook']
However we probably need to deduce the key name because it seems that is variable so the general correct form is:
$typeName = $data[$i]['type']
$data[$i][$typeName]['title']
$data[$i][$typeName]['number']['facebook']
which in the expanded form for facebook is : $data[$i][$data[$i]['type']]['number']['facebook']
and if the case is that the 'articles' are embedded as an array of objects, then:
$typeName = $data[$i]
$data[$i][$typeName][0]['title']
$data[$i][$typeName][0]['number']['facebook']
which in the expanded form for facebook is: $data[$i][$data[$i]['type']][0]['number']['facebook']
Here is a script that runs against those two json structures, and access their data:
<?php
//Two possible jsons, it seems that:
//1)the embedded article object can be an object or an array of objects so we try two different json structures
//2)also it seems that the key of the embedded object is variable and thus we need to take it every time from the type value (however in our examples it is always 'article'
$json1 = '[{"type":"article", "article":{"title":"hello","number":{"facebook":4,"twitter":6}}},{"type":"article","article":{"title":"hello","number":{"facebook":1,"twitter":3}}}]';
$json2 = '[{"type":"article", "article":[{"title":"hello","number":{"facebook":4,"twitter":6}}]},{"type":"article","article":[{"title":"hello","number":{"facebook":1,"twitter":3}}]}]';
$data1 = json_decode($json1, true);
$data2 = json_decode($json2, true);
for ($i=0; $i<count($data1); $i++) {
$articleType = $data1[$i]['type'];
echo 'Title from element: ',$i, ' ', $data1[$i][$articleType]['title'],
PHP_EOL;
echo 'Facebook number from element ', $i, ' ',
$data1[$i][$articleType]['number']['facebook'], PHP_EOL;
echo 'Twitter number from element ', $i, ' ',
$data1[$i][$articleType]['number']['twitter'], PHP_EOL;
}
//However if we have embedded an array of objects then we access this way (hardcoded to index 0)
$embIndex = 0;
for ($i=0; $i<count($data2); $i++) {
$articleType = $data2[$i]['type'];
echo 'Title from element ',$i, ' and embedded object index ',
$embIndex, ': ',$data2[$i][$articleType][$embIndex]['title'], PHP_EOL;
echo 'Facebook number from element ', $i, ' and embedded object index ',
$embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['facebook'], PHP_EOL;
echo 'Twitter number from element ', $i, ' and embedded object index ',
$embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['twitter'], PHP_EOL;
}
and the output:
Title from element: 0 hello
Facebook number from element 0 4
Twitter number from element 0 6
Title from element: 1 hello
Facebook number from element 1 1
Twitter number from element 1 3
Title from element 0 and embedded object index 0: hello
Facebook number from element 0 and embedded object index 0: 4
Twitter number from element 0 and embedded object index 0: 6
Title from element 1 and embedded object index 0: hello
Facebook number from element 1 and embedded object index 0: 1
Twitter number from element 1 and embedded object index 0: 3
['scores']come from?number, notscores$number = $data[0]['type'][$i]['number']['facebook'][0];…['facbook']is a number.[0]would only work on arrays or strings.$data[0]['type'][$i]['number']['facebook']has to be enough.