使用 php 检索 json 孩子(在另一个 foreach 中)



我正在尝试从 json 文件中检索一些数据,在这种情况下,json 文件有一个子调用"photo"并且该子存储多个文件名,我需要在外部 foreach 中获取这些文件名。这是我的代码:

我的 JSON 文件如下所示:

{
"nigiri": [{
"code": "NS-1",
"title": "Maguro",
"description": "6pc tuna",
"price": "$10.00",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"]
}, {
"title": "Scottish",
"code": "NS-2",
"price": "$9.50",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"],
"description": "6pc salmon"
}, {
"title": "Buri",
"code": "NS-3",
"price": "$10.00",
"photo": "NS-3.jpg",
"description": "6pc Hamachi"
}]
}

我的PHP文件看起来像这样:

<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type'])) 
{ 
header('Location: index.php');
exit; 
}
?>

// This way I only get the first value
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>

// I want to retrieve the others values from the "photo" child but still inside the previous foreach. I've try this:
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>

有时最好只停留在PHP中,而不是进出HTML:

foreach ($jsonfile->$type as $obj) {
echo "<h2>Name of the plate: $obj->title</h2>n";
$photos = is_array($obj->photo) ? $obj->photo : array($obj->photo);
foreach ($photos as $photo) 
echo "<img src="img/plates/thumbs/$photo" alt="$obj->title">n";
}

请注意,由于$photo可能不是一个数组,因此我们需要在尝试迭代它之前先检查它,如果不是,请将其制作成一个数组。

输出(对于您的示例数据,带$type = "nigiri"(:

<h2>Name of the plate: Maguro</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<h2>Name of the plate: Scottish</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<h2>Name of the plate: Buri</h2>
<img src="img/plates/thumbs/NS-3.jpg" alt="Buri">

你的意思是如果你有很多照片,你想要输出照片?

<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if (is_array($obj->photo)): ?>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>

<?php endforeach; ?>

必须使用嵌套循环。像这样:

<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type'])) 
{ 
header('Location: index.php');
exit; 
}
?>
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if(is_array($obj->photo)): ?>
<?php foreach ($obj->photo as $photo): ?>
<img src="img/plates/thumbs/<?php echo $photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else: ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>
<?php endforeach; ?>

最新更新