0

I have a script that get data from a JSON-API.

echo var_dump(json_decode($result, true)); this displays:

hejarray(1) { 
  ["items"]=> array(2) {
        ["item"]=> array(1) { 
              [0]=> array(23) { 
                    ["newsdeskML"]=> string(3) "2.1"                                                                           
                    ["type_of_media"]=> string(12) "pressrelease"       
                    ["language"]=> string(2) "sv" 
                    ["source_id"]=> string(5) "47784"             
                    ["source_name"]=> string(24) "Sverige AB" 
                    ["pressroom_name"]=> string(24) "Sverige AB" 
                    ["pressroom"]=> string(2) "se" 
                    ["pressroom_id"]=> string(5) "53128"             
                    ["organization_number"]=> string(11) "556052-5833" 
                    ["id"]=> string(6) "968485" 
                    ["url"]=> string(126) "xx.com" 
                    ["published_at"]=> string(19) "2014-03-05 08:08:33"               ["created_at"]=> string(19) "2014-03-05 08:08:33"       
                    ["updated_at"]=> string(19) "2014-03-05 08:08:34"       
                    ["header"]=> string(56) "header header" 
                    ["summary"]=> string(277) "text text".........

If i only want to echo the content in ["summary"]=> string(277) "text text" how can this be done?

EDIT Full json reponse, had to remove contest as it's sensetive data:

{  
   "items":{  
      "item":[  
         {  
            "newsdeskML":"2.1",
            "type_of_media":"pressrelease",
            "language":"sv",
            "source_id":"47784",
            "source_name":"",
            "pressroom_name":"",
            "pressroom":"se",
            "pressroom_id":"",
            "organization_number":"",
            "id":"968485",
            "url":"",
            "published_at":"2014-03-05 08:08:33",
            "created_at":"2014-03-05 08:08:33",
            "updated_at":"2014-03-05 08:08:34",
            "header":"",
            "summary":"",
            "body":""
         },
         {  
            "related_items":null
         }
      ]
   }
}

This gives the same php output i posted at first:

$var = json_decode($result, true); echo var_dump($var);

But echo var_dump($var['items']); gives null

2
  • it doesn't seem a valid json that try it here jsonformatter.curiousconcept.com Commented Aug 20, 2015 at 7:42
  • @Michelem changed to the correct one now, messed up copy pasting when i removed some data not to be shown. Commented Aug 20, 2015 at 7:45

2 Answers 2

4

You can get it with:

$var = json_decode($result, true);

echo $var['items']['item'][0]['summary']
Sign up to request clarification or add additional context in comments.

3 Comments

can you show us the $result string without the json_decode?
{ "items": { "item": [ { "summary": "text",
No please, edit your question and add the full string. (you can use pastebin.net if the string is really long)
2

json_decode returns an associative array when you specify the second parameter to true (like you did). So you can basically just go with:

$json_object = json_decode($result, true);
echo $json_object["path"]["to"]["your"]["property"];

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.