0

I apologize for the title beforehand, but wasn't even sure how to ask the question.

I am working with a json array from a youtube feed. The feed for a playlist and a user upload are formatted a little differently. In order to reduce the amount of code I want to define the elements based on the type of feed (playlist/user upload) and insert that into my foreach statement.

The foreach statement for user uploads looks like this:

foreach ($json_output->data->items as $data)    {}

and for the playlist it needs to look like this:

foreach ($json_output->data->items->video as $data)   {}

I have tried defining the statement in a variable beforehand and doing something as follows with no luck:

foreach ($json_output.$feedtype as $data)   {}

or

foreach ($json_output + $feedtype as $data)   {}

2 Answers 2

1

The easiest thing I could think of is to make an if condition on the feedtype:

$feedtype = 'uploads'; // or playlist

if ($feedtype == 'uploads') {
    $array = $json_output->data->items;
}
else if ($feedtype == 'playlist') {
    $array = $json_output->data->items->video;
}

foreach ($array as $data) {}
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't think the question through real well, but your response gave me an idea how to solve it.
0

$feed was already defined, so here is what I did.

foreach ( $json_output->data->items as $feed_data ){
if ($feed == 1) {$data = $feed_data;}
else {$data = $feed_data->video;}

1 Comment

yes..or even shorter: foreach ( $json_output->data->items as $data ){ if ($feed != 1) {$data = $data->video;}}

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.