简体   繁体   English

如何在PHP中迭代JSON数组并显示元素?

[英]How do I iterate JSON Array and display elements in PHP?

I'm trying to iterate a JSON Array and display the elements in DOM? 我正在尝试迭代JSON数组并在DOM中显示元素? My JSON Schema 我的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
  }
]

My PHP Code 我的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>';
    }
?>

Now I'm able to extract Objects from the Array, How do I iterate through this HTML? 现在,我可以从数组中提取对象,如何遍历此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>

You need to iterate over your JSON using a foreach loop, and escape the PHP tags so you can output the HTML. 您需要使用foreach循环遍历JSON,并转义PHP标记,以便输出HTML。

Example: 例:

$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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM