0

Here is my array that prints to screen.

    Array
( 
    [original_title] => My Title
    [genres] => Array
        (
            [0] => Array
                (
                    [id] => 18
                    [name] => Drama
                )

            [1] => Array
                (
                    [id] => 14
                    [name] => Fantasy
                )

            [2] => Array
                (
                    [id] => 10756
                    [name] => Indie
                )

        )

    [vote_average] => 6.8
    [vote_count] => 11
)

I am able to pull the values from the initial array like so:

echo"<br>Title: ";
print_r($pelinfo['original_title']);

Which outputs Title: My Title

I want to be able to add all genres by name.

Genres: Drama, Fantasy, Indie

Any help?

3 Answers 3

2

Take a look at control structures.

In this case, a foreach works nicely:

foreach($pelinfo['genres'] as $genre) {
   echo $genre['name'];
}

To output the exact format (with commas), you could build another array and use implode() (per Patrick Evans). My goal was to teach.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! that is understandable.
1
$Genres = array();
foreach( $pelinfo["genres"] as $Genre ) {
   $Genres[] = $Genre["name"];
}

echo "Genres:".implode(", ",$Genres);

Comments

0

Use foreach

foreach ($pelinfo['genres'] as $genre)
{
    echo $genre['name'];
}

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.