PHP parsing JSON

So i have a json structure (see $json), i am unsure how to loop through it in PHP to get the information i need.  Each time i do i will be looking to return the basic structure so in this case with the parameters i provided at the top of the code namely "US", "en", "primary" I want to return

[
                {
                    "title": "movie1",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ, PH, SG",
                    "category": "primary"
                },
                {
                    "title": "movie2",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ, PH, SG",
                    "category": "primary"
                }
]

but i am unsure how to do that.
$country = "US";
$language = "en";
$category = "primary";



$json ='
{
    "videos": [
        {
            "en": [
                {
                    "title": "movie1",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ, PH, SG",
                    "category": "primary" 
                },
                {
                    "title": "movie2",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ, PH, SG",
                    "category": "primary" 
                },
                {
                    "title": "movie3",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ",
                    "category": "secondary" 
                } 
            ],
            "es": [
                {
                    "title": "movie4",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ, PH, SG",
                    "category": "primary" 
                },
                {
                    "title": "movie5",
                    "url": "http://www.youtube.com",
                    "country": "CA, US, NZ",
                    "category": "secondary" 
                } 
            ] 
        } 
    ],
    "links": [
        
    ] 
}
';

$arr = json_decode($json,true);

foreach ($arr as $row)
{

}

Open in new window

LVL 4
Brant SnowAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
try this instead:
//...
$arr = json_decode($json,true);
$output=array();
foreach ($arr->videos as $video)
{
	if( isset($video->{$language}) )
	{
		foreach($video->{$language} as $item)
		{
			if( strpos($item->country,$country)!==FALSE && $item->category==$category)
			{
				$output[]=$item;
			}
		}
	}
}
echo json_encode($output);

Open in new window

0
 
hieloCommented:
try:

foreach ($arr->videos as $video)
{
	foreach($video->en as $item)
	{
		echo $item->title;
	}
}

Open in new window

0
 
Brant SnowAuthor Commented:
savant your second response is correct except for the

$arr = json_decode($json,true);  that creates an array format, but if you just take the true off then it works.
0
 
hieloCommented:
>>savant your second response is correct
Savant is the "rank"

Glad to help.

Regards,
Hielo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.