Jump to content

multidimensional array problrm


attaboy
 Share

Recommended Posts

I'm trying to create a multidimensional array of movies organized by genre. This should take the form of an associative array with genres as keys.  Each of the arrays elements should be an array containing movie names.

 

This is what I have:

<?php

$movies = array(

        array("SF" => "", "Terminator", "Soylent Green", "I Robot", "frankenstein"),

        array("horror" => "exorcist", "IT", "frankenstein"),

        array("comedy" => "holy grail", "blazing saddles", "young frankenstein")

    );

 

//echo $movies[0][0]."<br>";

//echo $movies[1][0]."<br>";

//echo $movies[2][0];

 

foreach ($movies as $c) {

        while (list($k, $v) = each ($c)) {

echo "$k ... $v <br/>";

        }

}

?>

 

this is the output:

SF ...

0 ... Terminator

1 ... Soylent Green

2 ... I Robot

3 ... frankenstein

horror ... exorcist

0 ... IT

1 ... frankenstein

comedy ... holy grail

0 ... blazing saddles

1 ... young frankenstein

 

you'll see Terminator is only listed as element 0 because there's an empty string in front of it. The first item in the array is liated as the value to the key while the second value is listed as element 0.  It doesn't seem right to me.

Link to comment
Share on other sites

Your array is nothing like what you have described your looking for.

 

$movies = array(
    "SF" => array("Terminator", "Soylent Green", "I Robot", "frankenstein"),
    "horror" => array("exorcist", "IT", "frankenstein"),
    "comedy" => array("holy grail", "blazing saddles", "young frankenstein")
);

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.