0

我在一篇文章中添加了许多缩略图,这是 Zend 框架的新内容,所以我不确定打印这个数组的编码风格。

我在 MongoDB 中的数组如下所示:

"thmbs": [
    {
        "src": "examp.com/img/product1.jpg",
        "caption": "Top View"
    },
    {
        "src": "examp.com/img/product2.jpg",
        "caption": "Side View"
    },
]

首先,我想检查数组以查看它是否包含任何内容,然后打印替代文本的图像和标题。这是正确的方向吗?

       <?php if ($this->thmbs) {
          foreach($this->thmbs->src as $val){?>
              <img id="lead-image" src="<?= $this->thmbs->src ?>"  alt="<?= $this->thmbs->caption?>"/>
       <?php }} ?>
4

1 回答 1

0

我对 MongoDB 不太熟悉,但如果它以 JSON 格式为您提供输出,只需使用json_decode(). 例如:

<?php

   $this->thmbs = json_decode($db_output, true);
   if($this->thmbs){
      foreach( $this->thmbs as $thmb ){
         echo "<img id='lead-image' src='". $thmb->src ."' alt='". $thmb->caption ."' />";
      }
   }

?>
于 2012-07-17T23:09:39.080 回答