0

I am trying to solve the following problem:

Note I am new to programming and PHP and working through SAMS PHP, MySQL and Apache (Julie C. Meloni).

There is an exercise where a multidimensional array has to be created. The outer array is an associative array listing the genres of movies. The genre arrays list movies of that genre. You are then asked to print out a list of genres with the list of films associated with that genre.

My code:

<?php 
$genres = array(
    'Science Fiction' => array('Star Trek', 'Star Wars', 'Alien'),
    'Drama' => array('Les Amant de Pont Neuf', 'War & Peace', 'Bridehead Revisited'),
    'Crime' => array('Heat', 'Pulp Fiction', 'Messerine')
);
$gKeys = array_keys($genres);
foreach ($gKeys as $genre) {
   print $genre."<br/>";

}
?>

This works and prints out:

Science Fiction

Drama

Crime

Here's where I am running into a wall. When I try adding another foreach loop after

print $genre;

no results appear in the browser (except results of first loop). I have tried everything. For example:

  • starting by using the array_value() function applied to $genre and then try a foreach on the array returned.
  • In the textbook there is also a while (list($x) = ($y)) mechanism mentioned.
  • I thoroughly re-read the array chapter and have looked elsewhere to no avail.

perhaps I have structured the multidimensional array incorrectly? Do the second dimensional arrays need to be associative arrays also for consistency?

1
  • what have you try to print more information ? Commented Mar 17, 2013 at 10:24

6 Answers 6

1

Your array is structured correctly. You're taking wrong array (array of just the keys) and adding another foreach loop after print $genre; hence it is not working.

No, it is not required for second dimensional arrays to be associative arrays as well.

<?php

$genres = array(
    'Science Fiction' => array('Star Trek', 'Star Wars', 'Alien'),
    'Drama' => array('Les Amant de Pont Neuf', 'War & Peace', 'Bridehead Revisited'),
    'Crime' => array('Heat', 'Pulp Fiction', 'Messerine')
);

foreach ($genres as $genre => $movies)
{
    print $genre . " - <br/>";
    foreach ($movies as $movie)
    {
        print $movie . "<br/>";
    }
    print "</br>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Wait! Why are you doing:

$gKeys = array_keys($genres);

This just gives you a single array of keys.

Do your first loop on $genres, then inside:

foreach ($genre as $key=>$subgenre)
{
   //do stuff here
}

1 Comment

Thanks ro ko and everybody else. Your quick replies have saved my sanity at a point where I am experiencing information overload learning a load of new things all together. I am looking through each solution to make sure I get how each approach works. Very instructive. Cheers!
0
foreach ($genres as $genrekey => $genrevalue) {
  print $genrekey."<br/>";
  foreach ($genrevalue as $value) {
    print $value."<br/>";
  }
}

Comments

0
    function multiarray_keys($ar) {

        foreach($ar as $k => $v) {
            $keys[] = $k;
            if (is_array($ar[$k]))
                $keys = array_merge($keys, multiarray_keys($ar[$k]));
        }
        return $keys;
    }


    $gKeys = multiarray_keys($genres);
echo "<pre>";
print_r(multiarray_keys($array));
echo "</pre>"; 

Comments

0

Your outer array is an associative array with genres as keys.

To print out movies of that genre (the subarrays), use another foreach as follows:

print "<ul>";
foreach ($genres as $genre => $movies) {
    print "<li>".$genre."<ul>";
    foreach ($movies as $movie) {
        print "<li>".$movie."</li>";
    }
    print "</ul></li>";
}
print "</ul>";

Comments

0

If you need this for seeing what data goes in an array for debugging purposes you could use PHPs var_export(), var_dump(), print_r() only to make sure the data is getting populated as you want it to. Otherwise if you want to show it in production you could do some like this:

foreach($multiDimentinalArray as $key => $value):
     if(is_array($value)):
         foreach($value as $k => $val):
              print "    $key.$k => $val";
     else:
          print "$key => $value";
     endif;
endforeach;

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.