Jump to content

Recommended Posts

I'm a bit stuck on this question

 

1, Create a multidimensional array of movies organized 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 ("2001", "Alien", "Terminator", and so on).

 

2, Loop through the array you created in exercise 1, outputting each genre and its associated movies to the browser.

 

I dunno how to get it so it displays something like:

 

Horror

- list of horror films

 

War

-List of war films

 

 

my array looks like this. Probably wrong to do what the question says

 

$films = array(

array(

"sf"=>"Alien",

"horror"=>"Hellraiser",

"war"=>"platoon",

),

array(

"sf"=>"star wars",

"horror"=>"dawn of the dead",

"war"=>"1942",

),

 

);

Link to comment
https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/
Share on other sites

Well, this is obviously an assignment, so I will not give you the solution. But, your array is incorrect to begin with. It states that "This should take the form of an associative array with genres as keys "

 

Your example above has no defined keys for the topmost array elements - you simply created sub-arrays which will have the indexes 0, 1, 2, etc.

 

The array should look more like this:

 

$films = array (
  'Horror' => array ('film1', 'film2', 'film3'),
  'Drama' => array ('film1', 'film2', 'film3'),
  'Action' => array ('film1', 'film2', 'film3')
);

not really assignment. learning using the book teach yourself php!!

ok sorted it out :

managed to sort the topics but i was wondering if it were possible to do another sort for the film names within each topic

 

 

<?php

 

 

$films = array(

"Horror" => array(

"Hellraiser",

"Dawn Of The Dead",

"The Evil Dead",

),

"War"=>array(

"Saving Private Ryan",

"Platoon",

"1942",

),

"Action"=>array(

"James Bond",

"Die Hard",

"Terminator",

"The Matrix",

),

 

);

 

 

 

ksort ($films);

 

 

foreach ($films as $title => $items){

print "<p>";

print "<u>$title</u><br/>";

 

foreach ($items as $key=> $name ) {

print "$name<br/>";

 

}

print "</p>";

}

 

?>

</div>

</body>

</html>

 

 

output:

 

Action

James Bond

Die Hard

Terminator

The Matrix

 

Horror

Hellraiser

Dawn Of The Dead

The Evil Dead

 

War

Saving Private Ryan

Platoon

1942

 

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.