jonnypixel
jonnypixel

Reputation: 327

How to retrieve values from multilevel json

Im trying to get the values from the various sections of this json entry.

{
    "gallery_show": "1",
    "gallery": {
        "path": ["images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg", "images\/slide2.jpg", "images\/slide1.jpg"],
        "title": ["", "", "", "", "", ""],
        "caption": ["", "", "", "", "", ""],
        "thumb": ["\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg", 
        "\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg", 
        "\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg", 
        "\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg", 
        "\/admin\/cache\/thumbs_200x150\/slide2_200x150.jpg", 
        "\/admin\/cache\/thumbs_200x150\/slide1_200x150.jpg"]
    },
    "videos_show": "1",
    "videos": {
        "title": ["Arnie", "Arnie", "Arnie", "Arnie", "Arnie"],
        "sharelink": ["http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY", "http:\/\/youtu.be\/7kTz6MVrBlY"]
    },
    "attachments_show": "1",
    "attachments": {
        "path": ["images\/slide2.jpg", "images\/slide1.jpg"],
        "title": ["Attchment", "Attchment"],
        "caption": ["Attachment Description", "Attachment Description"]
    },
    "links_show": "1",
    "links": {
        "title": ["Link1", "Link2"],
        "httplink": ["http:\/\/www.mydomain.com", "http:\/\/www.mydomain.com"],
        "targetlink": ["_blank", "_blank"]
    }
}

If i use this method

$entries= json_decode($this->item->entries);

and then i echo this

echo $entries->gallery_show;

I get the desired result = 1

But how do i now get the things beneath it if gallery_show=1 and display them as values i can use inside my php?

Any help for this old brain would be much appreciated.

Cheers in advance Jonny

Upvotes: 1

Views: 53

Answers (1)

You could do somewhat like this...

$arr = json_decode($json);
foreach($arr as $k=>$v)
{
    if($k=='gallery_show' && $v==1)
    {
        foreach($arr->gallery->path as $path)
        {
            echo "<img src=$path />";
        }
        break;
    }
}

Demo

Code update..

$arr = json_decode($json);
$i=0;
foreach($arr as $k=>$v)
{
    if($k=='gallery_show' && $v==1)
    {

        foreach($arr->gallery as $gall)
        {

            echo "Title :".$arr->gallery->title[$i]."<br/>";
            echo "Caption :".$arr->gallery->caption[$i]."<br/>";
            echo "Thumbnail :".$arr->gallery->thumb[$i]."<br>";
            echo "<img src=".$arr->gallery->path[$i]." /><br>";
            $i++;
        }
        break;
    }
}

Demo

Upvotes: 1

Related Questions