简体   繁体   English

用PHP多维数组的结果回显

[英]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. 这是给teamtreehouse.com的,所以我想我缺少一些简单的东西。

Try below code: 试试下面的代码:

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

The problem is You have not create any array key is naming "0" 问题是您尚未创建任何命名为“ 0”的数组键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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