View Full Version : PHP4 array help
Shalazar
10-09-2000, 02:43 AM
I'm working through a book on learning PHP4, and am having trouble on one of the execises.[nbsp][nbsp]So if anyone could provide help, feel free:
Question:[nbsp][nbsp]Create a multidimensional array of movies organized by genre.[nbsp][nbsp]This should take the form of an associative array with genres as keys ("Sci-Fi", "Action", "Romance", etc.).[nbsp][nbsp]Each of this associative array's elements should be an array containing movie names ("2001", "Alien", "Terminator", etc.).[nbsp][nbsp]
heath
10-09-2000, 04:27 AM
Question:[nbsp][nbsp]Create a multidimensional array of movies organized by genre.[nbsp][nbsp]This should take the form of an associative array with genres as keys ("Sci-Fi", "Action", "Romance", etc.).[nbsp][nbsp]Each of this associative array's elements should be an array containing movie names ("2001", "Alien", "Terminator", etc.).
It helps me if I think of arrays as x,y co-ordinates on a map.[nbsp][nbsp]Wherever the first and second element of the array intersect, there is the value. i.e. (per your example):
lets call our piece of graph paper (the array):
$array_of_movies_organized_by_genre
'x' will increase once for every movie, starting at 0, and increment to 1 then 2, then 3, etc.
'y' will fluctuate a little, so but not as much as 'x'.[nbsp][nbsp]It will be our movie type, and we'll call it 'action', or 'romance' - whichever is applicable
We can now populate our graph, or array:
$array_of_movies_organized_by_genre[0]['action'] = "INDY JONES";
$array_of_movies_organized_by_genre[1]['action'] = "RAMBO";
$array_of_movies_organized_by_genre[0]['romance'] = "PULP FICTION";
A visual interpretation of our array/graph now looks like this:
Array
(
[nbsp][nbsp][nbsp][nbsp][0] => Array
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp](
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][action] => INDY JONES
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][romance] => PULP FICTION
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp])
[nbsp][nbsp][nbsp][nbsp][1] => Array
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp](
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][action] => RAMBO
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp])
)
You are prolly more confused now than you were before.[nbsp][nbsp]Hope this helps...
Heath
heath
10-09-2000, 04:29 AM
By the way, who is Charisma Carpenter?
Shalazar
10-09-2000, 03:44 PM
I think I may have the solution.[nbsp][nbsp]Previous examples never showed anything like this, but it appears to work from testings.[nbsp][nbsp]Anything can contain an array is the secret:
<?php
$movies = array (
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]action => array ( "Terminator" , "Indiana Jones" , "Abyss" ),
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]drama => array ( "Erin Brokovich" , "28 Days" ),
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]sci-fi => array ( "Fifth Element" , "Star Wars" , "Star Trek" ),
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]romance => array ( "Shakespere in Love" )
[nbsp][nbsp][nbsp][nbsp][nbsp]);
And Charisma Carpenter is an actress, appearing on the WB Network's Buffy the Vampire Slayer, and now Angel.
Shalazar
10-10-2000, 03:09 AM
Hmmm, now how would I loop through this using the foreach statement to print each genre and its associated movies to the browser?
Shalazar
10-12-2000, 02:17 PM
In a continuing effort to answer my own questions:
Looping through these arrays:
ksort ($movies);
foreach ($movies as $genre=>$genrearray) {
[nbsp][nbsp]print "<UL><u>$genre</u>";
[nbsp][nbsp]asort($genrearray);
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] foreach ($genrearray as $title) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]print " $title";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]};
[nbsp][nbsp]print "[/list]";
};
Managed to even alphabetically sort the genres and the movie titles :)[nbsp][nbsp]Yeah me!
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.