在 PHP 中使用 foreach 语句回显一个 3 维数组

我已经看到了所有与我相关的问题,但仍然没有找到解决我的“简单”问题的方法。我有一个 3 维数组,我只想显示结果。但是当我回应时,. "<td>".$person ['job']."</td>"我收到了错误。任何建议thnx


$people= array(


array(

    "name" => "Jennifer Kimbers",

    "age"=>"45",

    "email" => "abc@gmail.com",

    "city" => "Seattle",

    "state" => "Washington"),

        array(

            "job"=>"web developer"

        ),


array(

    "name" => "Rodney Hutchers",

    "age"=>"55",

    "email" => "def@gmail.com",

    "city" => "Los Angeles",

    "state" => "California"),

        array(

             "job"=>"data developer"

         );


echo "<table>"

    ."<th>FullName</th>"

    ."<th>Age</th>"

    ."<th>Email</th>"

    ."<th>City</th>"

    ."<th>State</th>"

    ."<th>Job</th>";


 foreach ($people as $person) {

    echo "<tr>"

        . "<td>" . $person ['name'] . "</td>"

        . "<td>" . $person ['age'] . "</td>"

        . "<td>" . $person ['email'] . "</td>"

        . "<td>" . $person ['city'] . "</td>"

        . "<td>" . $person ['state'] . "</td>"

        . "<td>" . $person ['job'] . "</td>"

        . "</tr>";


}

echo "</table>";


梵蒂冈之花
浏览 86回答 1
1回答

翻过高山走不出你

您的数组结构略有偏差,在array(&nbsp; &nbsp; "name" => "Jennifer Kimbers",&nbsp; &nbsp; "age"=>"45",&nbsp; &nbsp; "email" => "abc@gmail.com",&nbsp; &nbsp; "city" => "Seattle",&nbsp; &nbsp; "state" => "Washington"),&nbsp; // Close bracket here&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "job"=>"web developer"&nbsp; &nbsp; &nbsp; &nbsp; ),这个缩进正确的是array(&nbsp; &nbsp; "name" => "Jennifer Kimbers",&nbsp; &nbsp; "age"=>"45",&nbsp; &nbsp; "email" => "abc@gmail.com",&nbsp; &nbsp; "city" => "Seattle",&nbsp; &nbsp; "state" => "Washington"),array(&nbsp; &nbsp; "job"=>"web developer"),所以你的循环试图将它用作两个单独的数据位,而第二个不包含你期望的很多字段。您需要确保在正确的位置关闭数组元素/将作业添加到与其余数据相同的元素中......array(&nbsp; &nbsp; "name" => "Jennifer Kimbers",&nbsp; &nbsp; "age"=>"45",&nbsp; &nbsp; "email" => "abc@gmail.com",&nbsp; &nbsp; "city" => "Seattle",&nbsp; &nbsp; "state" => "Washington",&nbsp; // Move ) after the job&nbsp; &nbsp; "job" => "web developer"),如果您需要额外级别的数组,那么您可以将其列为工作列表......array(&nbsp; &nbsp; "name" => "Jennifer Kimbers",&nbsp; &nbsp; "age"=>"45",&nbsp; &nbsp; "email" => "abc@gmail.com",&nbsp; &nbsp; "city" => "Seattle",&nbsp; &nbsp; "state" => "Washington",&nbsp; &nbsp; "jobs" => array( "title" => "web developer")),显示它们foreach ($people as $person) {&nbsp; &nbsp; echo "<tr>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>" . $person ['name'] . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>" . $person ['age'] . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>" . $person ['email'] . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>" . $person ['city'] . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>" . $person ['state'] . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "<td>";&nbsp; &nbsp; foreach ( $person['jobs'] as $job ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $job . "/";&nbsp; &nbsp; }&nbsp; &nbsp; echo "</td>"&nbsp; &nbsp; &nbsp; &nbsp; . "</tr>";}虽然这样你最终会/在职位名称后面有一个尾随,但它显示了原则。您可以代替内部foreach()循环使用...echo implode("/", $person['jobs']);
打开App,查看更多内容
随时随地看视频慕课网APP