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

时间:2018-10-02 02:21:28

标签: php json

我正在尝试从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; ?>

3 个答案:

答案 0 :(得分:0)

如果您有很多照片,您是要输出照片吗?

<?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; ?>

答案 1 :(得分:0)

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

<?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; ?>

答案 2 :(得分:0)

有时候最好只保留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">