简体   繁体   中英

Echo with the result of a multidimensional array in PHP

This is my code:

<?php
$contacts = array($alena = array("name" => "Alena Holligan",
                                 "email" => "alena.holligan@teamtreehouse.com"), 
                  $dave = array("name" => "Dave McFarland",
                                "email" => "dave.mcfarland@teamtreehouse.com"), 
                  $treasure = array("name" => "Treasure Porth",
                                    "email" => "treasure.porth@teamtreehouse.com"), 
                  $andrew = array("name" => "Andrew Chalkley",
                                  "email" => "andrew.chalkley@teamtreehouse.com")
                 );

    echo "<ul>\n";
names.
    echo "<li>$contacts[0][name]</li>\n";
    echo "</ul>\n";
?>

I get this error when I try to view a webpage running this script:

Notice: Array to string conversion in index.php on line 15
Array[name]

Is there a built in function to convert this array to a string, or do I have a syntax error?

This is for teamtreehouse.com, so I assume I'm missing something simple.

Try below code:

<?php echo $contacts[$alena]['name']; ?>

The problem is You have not create any array key is naming "0"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM