<html>
<head>
<title>arrays-1</title>
</head>
<body>
<?php

$characters = array (
            array (name=>"bob",
                   occupation=>"superhero",
                   age=>30,
                   specialty=>"x-ray vision"),
            array (name=>"sally",
                   occupation=>"superhero",
                   age=>24,
                   specialty=>"superhuman strength"),
            array (name=>"mary",
                   occupation=>"arch villian",
                   age=>63,
                   specialty=>"nanotechnology"),
                   );
echo $characters[0][occupation];
// prints "superhero"
?>
</body>
</html>

????

Thanks.

    my guess, remove the comma here...

    "nanotechnology"),);"

    also would recommend using ['occupation'] for an associative array index

      [QUOTEmy guess, remove the comma here...

      "nanotechnology"),);"

      also would recommend using ['occupation'] for an associative array index
      [/QUOTE]

      Thanks for the quick reply....but still not working. Its giving me a parse error on line 22 - which is the echo line...

        did you add the ' ' around occupation also?

        if not... then that could be the parse error... otherwise...

        try echo "$characters[0]['occupation']";

        last ditch....
        on the line before the ec ho...

        do

        echo '<pre>';
        print_r($characters);
        echo '</pre>';
        

          Here is the script as it appears after you suggestions.

          <html>
          <head>
          <title>arrays-1</title>
          </head>
          <body>
          <?php
          
          $characters = array (
                      array (name=>"bob",
                             occupation=>"superhero",
                             age=>30,
                             specialty=>"x-ray vision"),
                      array (name=>"sally",
                             occupation=>"superhero",
                             age=>24,
                             specialty=>"superhuman strength"),
                      array (name=>"mary",
                             occupation=>"arch villian",
                             age=>63,
                             specialty=>"nanotechnology")
                             );
          echo $characters[0]['occupation'];
          // prints "superhero"
          ?>
          </body>
          </html>

          I'm a newbie and learning out of a book....I also have a question about the following array which comes from the php manual.

          <?php
          $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
          
          echo $arr["somearray"][6];    // 5
          echo $arr["somearray"][13];  // 9
          echo $arr["somearray"]["a"];  // 42
          ?> 

          Am I correct in thinking:

          the variable $arr contains an array within an array...."somearray is referring to what..??

          I'm just trying to learn what refers what??

          Thanks.

          Adrian

            somearray would be an associative name to an array, ie instead of being called by [0] or [9] you would call it by ['associative_name']

              8 days later

              OK.....I have a new array that is giving me parsing errors. The below code is a multi-dimensional assosciative array that I'm trying to loop through....it gives me the parse error on line 9 - the line that says," array = ( title=>"t2",".

              I'm learning here - so if this script can be corrected and made easier to understand by coding it differently, I would greatly appreciate it.....

              The thing that frustrates me is that everything looks right!...and it doesn't work.

              Thanks.

              <?php
              
              $movies = array (
                       array = ( title=>"t2",
                                 genre=>"action"),
                       array = ( title=>"casablanca",
                                 genre=>"romance"),
                       array = ( title=>"About Polly",
                                 genre=>"Comedy")
                                 );
              foreach ($movies as $val)
                   {
                      foreach ($val as $key=>$final_val)
                      {
                      print "$key:$final_val<br>";
                      }
                   print "<br>";
                   }
              ?>
              </body>
              </html>

                should be array ('name' => "string value");

                  Try putting the following code in your browser and see if it works.....

                  <?php
                  
                  $movies = array (
                           array = ('title'=>"t2",'genre'=>"action",
                           array = 'title'=>"casablanca",'genre'=>"romance",
                           array = 'title'=>"About Polly",'genre'=>"Comedy")
                                     );
                  foreach ($movies as $val)
                       {
                          foreach ($val as $key=>$final_val)
                          {
                          print "$key:$final_val<br>";
                          }
                       print "<br>";
                       }
                  ?>

                  Thanks.

                    here is the correct code.
                    My problem was that I had:

                    array = (key=>"string"); 

                    when it should have been:

                    [code=php]array (key=>"string"); 

                    Here is the whole piece of code:

                    <?php
                    
                    $movies = array (
                             array (title=>"t2",genre=>"action"),
                             array (title=>"casablanca",genre=>"romance"),
                             array (title=>"About Polly",genre=>"Comedy")
                                       );
                    foreach ($movies as $val)
                         {
                            foreach ($val as $key=>$final_val)
                            {
                            print "$key:$final_val<br>";
                            }
                         print "<br>";
                         }
                    ?>
                      Write a Reply...