Jump to content

Recommended Posts

I'm studying PHP on my own and have came across an exercise that I'm 99% done with! After looking at the excercise in question, please help with a simple solution. All I want to do is print my genre (can't do) with its associated movies to the browser (can do, thats the easy part)

 

EXERCISE:

Create a multidimensional array of movies by genre. This should take the form of an associative array with genres as keys ("SF", "Action", "Romance", and so on). Each of this associative array's elements should be an array containing movie names ("Superbad", "Alien", "Terminator", and so on).

 

<?php

 

$genres = array (

    $comedy=array("Superbad", "Yes Man", "Anchor Man"),

    $horror=array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"),

    );

 

foreach($genres as $section){

    print "<p>";

    foreach ($section as $subject=> $value){

    static $subject=0;

    $subject++;

    print "$subject: $value<br />";

    }

print "</p>";

}

 

 

?>

 

If you preview this on your own system, you see it's pretty simple, each element in the array is part of a numbered list. But what I WANT to do, is each time the loops goes through to print the name of the genres they are in!!!! Please Help. I thank you in advance. Your help is very much appreciated.

 

- Adam

Link to comment
https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/
Share on other sites

You didn't set up the keys correctly in the array.

 

<?php

$genres = array (
     'Comedy' => array("Superbad", "Yes Man", "Anchor Man"),
     'Horror'    => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"),
    );

foreach($genres as $genre){
    echo "<p>$genre:</p>\n<p>";
    foreach ($genre as $num => $movie) {
        echo "$num: $movie<br />\n";
    }
    echo "</p><br />";
}

 

Should be good.

should use for... not foreach for the second one. 

 

and get rid of the $ before 'comedy' and 'horror' and echo $subject; 

 

$genres = array (

    'comedy' =>array("Superbad", "Yes Man", "Anchor Man"),

    'horror' =>array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"),

    );

This works:

 

$movies = array(

'Comedy' => array(
	'Superbad',
	'Yes Man',
	'Anchor Man',
),

'Horror' => array(
	'Texas Chainsaw',
	'Saw 2'
),

'Romance' => array(
	'Romance movie 1',
	'Romance movie 2'
)

);

foreach($movies as $genre => $movie_list)
{
foreach($movie_list as $movie)
{
	echo $movie . ' ( ' . $genre . ')<br />';
}
}

Ok, I can see that some of you are trying to help me out. Can one of you give me the entire code I will need inside my php tags? To reitterate, I want the outcome to look something like this:

 

Horror

1. Slither

2. etc.

3. etc.

 

Comedy

1. Superbad

2. Anchorman

3. etc.

<?php

$genres = array (
     'Comedy' => array("Superbad", "Yes Man", "Anchor Man"),
     'Horror'    => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"),
    );

foreach($genres as $genre){
    echo "<p>$genre:</p>\n<p>";
    $i=1;
    foreach ($genre as $num => $movie) {
        echo "{$i}: {$movie}<br />\n";
        $i++;
    }
    echo "</p><br />";
}
?>

 

Should do it.

No Premiso, when I run your script I get:

 

Array:

 

1: Superbad

2: Yes Man

3: Anchor Man

 

Array:

 

1: Texas Chainsaw Massacre

2: People Under The Stairs

3: Slither

 

-- I want it to say the name of the array (such as Comedy, and Horror above the second batch) I don't want it to just say "Array". Any more help please?

My bad.

 

<?php

$genres = array (
     'Comedy' => array("Superbad", "Yes Man", "Anchor Man"),
     'Horror'    => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"),
    );

foreach($genres as $genre => $movies){
    echo "<p>$genre:</p>\n<p>";
    $i=1;
    foreach ($movies as $num => $movie) {
        echo "{$i}: {$movie}<br />\n";
        $i++;
    }
    echo "</p><br />";
}
?>

 

Should take of it. Sorry about that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.