本文介绍了如何使用php循环或显示json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用PHP显示来自JSON的技能和图像,但我无法研究
I want to display skills and image from JSON using PHP I'm trying to research but not working
错误消息:
注意:第27行的C:\ xampp \ htdocs \ test \ view.php中的数组到字符串的转换
注意:第32行的C:\ xampp \ htdocs \ test \ view.php中的数组到字符串的转换
data.json
data.json
{
"UserData": [
{
"id": "name",
"fname": "Joey",
"lname": "Tulang",
"age": "29",
"gender": "Male",
"skills": [ "HTML", "CSS", "JavaScript" ],
"image": [ "html.jpg", "css.jpg", "js.jpg" ]
},
{
"id": "2",
"fname": "Angelica",
"lname": "Balce",
"age": "20",
"gender": "Female",
"skills": [ "C++", "Python", "PHP"],
"image": [ "c++.jpg", "python.jpg", "php.jpg" ]
},
{
"id": "3",
"fname": "Mj",
"lname": "King",
"age": "21",
"gender": "male",
"skills": [ "Photoshop", "Figma", "Corel Draw"],
"image": [ "photoshop.jpg", "figma.jpg", "cdraw.jpg" ]
}
]
}
index.php
index.php
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<?php
$jsondata = file_get_contents("data.json");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach ( $json['UserData'] as $display ) {
$output .="<li>" .$display['fname']. " <a href='view.php?id=".$display['id']."'>View User Data</a></li> </br>";
}
$output .= "</ul>";
echo $output;
?>
</body>
</html>
view.php
<?php
$jsondata = file_get_contents("data.json");
$json = json_decode($jsondata, true);
$key = array_search((int) $_GET['id'], array_column($json['UserData'], 'id'));
// if $key false, was not found, presume it was...
$user = $json['UserData'][$key];
?>
<!DOCTYPE html>
<html>
<head>
<title>View User Data</title>
</head>
<body>
<!-- Display User Data Here -->
<p>First Name: <?= $user['fname'] ?></p>
<p>Last Name: <?= $user['lname'] ?></p>
<p>Age: <?= $user['age'] ?></p>
<p>Gender: <?= $user['gender'] ?></p>
<p>Skills</p>
<ul>
<?= "<li>" .$user['skills']. "</li>" ?>
</ul>
<p>Image</p>
<?= "<imge src='image/" .$user['image']. "'/>" ?>
</body>
</html>
view.php(输出)
view.php (Output)
推荐答案
答案:
foreach ($user['image'] as $value)
{
echo "<img src='".$value."'>";
}
这篇关于如何使用php循环或显示json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!