我正在尝试显示姓名和姓氏的列表,但会不断出现错误。
我的代码:
(。json)
{
"region":"Australia",
"male":[
"Benjamin",
"Charlie",
"Cooper",
"Daniel",
"Ethan",
"Harriso",
"Harrison",
"Harry",
"Henry",
"Jack",
"Jacob",
"James",
"Joshua",
"Lachlan",
"Levi",
"Liam",
"Lucas",
"Mason",
"Max",
"Michael",
"Noah",
"Oliver",
"Riley",
"Ryan",
"Samuel",
"Thomas",
"Tyler",
"William"
],
"female":[
"Amelia",
"Ava",
"Charlotte",
"Chloe",
"Ella",
"Emily",
"Grace",
"Isabella",
"Jasmine",
"Lily",
"Lucy",
"Matilda",
"Mia",
"Olivia",
"Ruby",
"Sienna",
"Sophia",
"Sophie",
"Summer",
"Zoe"
],
"surnames":[
"Smith",
"Jones",
"Williams",
"Brown",
"Wilson",
"Taylor",
"Morton",
"White",
"Martin",
"Anderson",
"Thompson",
"Nguyen",
"Thomas",
"Walker",
"Harris",
"Lee",
"Ryan",
"Robinson",
"Kelly",
"King"
]
},
我的php代码:
<?php
/**
* Get the data from a JSON file.
*
* @return string
*/
function getData($filename)
{
$json = file_get_contents(__DIR__ . '/names.json');;
return (json_decode($json, true));;
}
echo '<ul>';
$array =getData('names.json'); foreach($array as $names){
echo '<li>';
echo $names;
echo '</li>';
}
echo '</ol>';
错误:
“警告:为中的foreach()提供了无效的参数 C:\ xampp \ htdocs \ wordpress \ wp-content \ themes \ blank \ index.php在线 19“
我的目标是显示10个名字+姓氏的列表,对您的帮助非常感谢。