大家好,我只是想知道如何使用 foreach 打印出一组数组?这是我的简单代码。
<?php
function names_job() {
$names = array();
$names['lester'] = array('web developer' => 'name1', 'web designer' => 'name2', 'eating' => 'name2');
$names['joanna'] = 'web designer';
$names ['chloe'] = 'student';
return $names;
}
function display_arr() {
$names_jobs = names_job();
foreach( $names_jobs as $name => $job ) {
echo '<br>Name: ' . $name . ' Job: ' . $job . '<br>';
foreach( $job as $jobs => $boss ) {
echo '<br>-- ' . $jobs . ' ' . $boss . '<br>';
}
}
}
display_arr();
?>
上面代码的结果是这样的:
Name: lester Job: Array
web developer name1
web designer name2
eating name2
Name: joanna Job: web designer
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\code\index.php on line 21
Name: chloe Job: student
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\code\index.php on line 21