0

I Have a problem for using a json feed with php. For example :

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":4,
"twitter":6}
}],

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":1,
"twitter":3}
}],

I have no problem to save the title :

$titre = $data[$i]['type'][0]['title'];

But i can't find how to save the facebook number. I have tried a lot of combinaison

$number = $data[0]['type'][$i]['scores']['facebook'][0];

OR

$number = $data[0]['type'][$i]['scores']['facebook'];

OR

$number = $data[0]['type'][$i]['scores']['facebook'][1];

No one works... Do you have an idea ? Many thanks guys !

6
  • Where does ['scores'] come from? Commented Jul 22, 2013 at 12:56
  • The key is number, not scores Commented Jul 22, 2013 at 12:56
  • $number = $data[0]['type'][$i]['number']['facebook'][0]; Commented Jul 22, 2013 at 12:57
  • Yep sorry, you're right. It's number, not score. But $number = $data[0]['type'][$i]['number']['facebook'][0]; doesnt work... I don't understand why. Because everything is ok for the title... Commented Jul 22, 2013 at 13:03
  • @ThomasSavigny: …['facbook'] is a number. [0] would only work on arrays or strings. $data[0]['type'][$i]['number']['facebook'] has to be enough. Commented Jul 22, 2013 at 13:04

3 Answers 3

2

Your json is invalid.

http://php.net/manual/en/function.json-last-error.php

$json = '[{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":4,
  "twitter":6}
  }],

  [{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":1,
  "twitter":3}
  }]';
$array = json_decode($json, true);

switch (json_last_error()) {
  case JSON_ERROR_NONE:
    echo ' - No errors';
    break;
  case JSON_ERROR_DEPTH:
    echo ' - Maximum stack depth exceeded';
    break;
  case JSON_ERROR_STATE_MISMATCH:
    echo ' - Underflow or the modes mismatch';
    break;
  case JSON_ERROR_CTRL_CHAR:
    echo ' - Unexpected control character found';
    break;
  case JSON_ERROR_SYNTAX:
    echo ' - Syntax error, malformed JSON';
    break;
  case JSON_ERROR_UTF8:
    echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
  default:
    echo ' - Unknown error';
    break;
}

print_r($array);
  • Syntax error, malformed JSON
Sign up to request clarification or add additional context in comments.

2 Comments

I assume the OP only gave a snippet of his JSON
Yes :) It's just a snippet. Thanks you both !
0

Try using json_decode() function (php manual entry). In this way you can easily access the values of a json and avoid headaches on trying to find the values on a array.

This will also return NULL if your Json is invalid or have a problem (which helps to debug)

3 Comments

It can be safely assumed than the json_decode function has already been used in order to have obtained a PHP array from JSON data, therefore this does NOT answer the question.
Unless stated otherwise, I don't assume anything. If he used the function the malformed json should also be easy to noticed because of the Null response. Therefore I am not assuming it and trying to help him.
THanks Fgitz, bus as SirDarius said, I use json_decode before my php.
0

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:

  1. 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.
  2. 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

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.