【问题标题】:How do I iterate JSON Array and display elements in PHP? [duplicate]如何在 PHP 中迭代 JSON 数组并显示元素? [复制]
【发布时间】:2019-01-08 23:20:23
【问题描述】:

我正在尝试迭代 JSON 数组并在 DOM 中显示元素? 我的 JSON 架构

[
  {
    "pic": "<?php echo base_url();?>assets/images/testi_pic1.jpg",
    "desc": "My experience with Vestedu has been great. I was able to save money on my student loan, they have competitive rates, excellent customer service and easy application process",
    "name": "Ram Chandra",
    "degree": "MBA",
    "desig": "designation"
  },
  {
    "pic": "<?php echo base_url();?>assets/images/testi_pic2.jpg",
    "desc": "My experience with Vestedu has been great. I was able to save money on my student loan, they have competitive rates, excellent customer service and easy application process",
    "name": "Ram Chandra",
    "degree": "MBA",
    "desig": null
  }
]

我的 PHP 代码

<?php
    $str    =   file_get_contents(base_url(). 'assets/data.json');
    $json   = json_decode($str, true); // decode the JSON into an associative array

    $count  = count($json);
    for ($i=0; $i < $count; $i++) {
        echo '<pre>' . print_r($json[$i], true) . '</pre>';
    }
?>

现在我可以从数组中提取对象,我该如何遍历这个 HTML?

<div class="carousel-inner" role="listbox">
    <div class="item active">
        <div class="testimonialbox">
            <img src="<?php echo base_url();?>assets/images/testi_pic1.jpg" alt="testimonial" />
            <p>"My experience with Vestedu has been great. I was able to save money on my student loan, they have competitive rates, excellent customer service and easy application process"</p>
            <div class="testimonial-author-box">
                <i class="fa fa-quote-right" aria-hidden="true"></i>
                <h3>Ram Chandra</h3>
                <span>MBA</span>
            </div>
        </div>
    </div>

【问题讨论】:

  • 改用foreach循环。
  • 这个问题是如何再次从 JSON 跳到 HTML 的?
  • @mario 我想在 PHP 内部的 HTML 中呈现 JSON 对象
  • 所以echo "&lt;h3&gt;{$json[$i]["name"]}&lt;/h3&gt;"; 基本上? (顺便说一句,字符串中带有&lt;?php echo base_url 的蠕虫罐头是另一个问题。)

标签: php html json dom


【解决方案1】:

您需要使用 foreach 循环遍历您的 JSON,并转义 PHP 标记以便输出 HTML。

示例:

$str    = file_get_contents(base_url(). 'assets/data.json');
$json   = json_decode($str, true); // decode the JSON into an associative array

// I assume this is the container, so output that:
echo '<div class="carousel-inner" role="listbox">';

// Now loop through your array, and reference each value you need:
foreach ($json as $value): 
  ?>

    <div class="item active">
      <div class="testimonialbox">
        <img src="<?php echo $value['pic']; ?>" alt="testimonial" />
        <p><?php echo $value['desc']; ?></p>
        <div class="testimonial-author-box">
          <i class="fa fa-quote-right" aria-hidden="true"></i>
          <h3><?php echo $value['name']; ?></h3>
          <span><?php echo $value['degree']; ?></span>
        </div>
      </div>
    </div>

  <?php
endforeach;

// Close the container
echo '</div>';

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 2015-09-11
    • 2011-04-06
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多