duancheng1955
2015-05-29 11:46
浏览 122
已采纳

在json编码数组中的foreach循环

I am sending preformatted HTMl with AJAX JSON, JSON have below code,

I am trying pull data array from DB and echoing array data, I am not able to put foreach loop in json_encode, because seems my code is wrong at foreach loop,

How can i achieve that?

echo json_encode(array('returnnews' => '<div class="news-item-page">
                                    <h3 class="text-info" style="margin-top:0">'.$latestnews->news_subject.'</h3>
                                    '.$latestnews->news_content.'


                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <ul class="list-inline blog-tags">
                                            <li>
                                                <i class="fa fa-tags"></i>'.
                                                foreach($news_tag_array as $tag){
                                                <a href="javascript:;">
                                                echo $tag </a>
                                               }

                                            </li>
                                        </ul>
                                    </div>
                               </div>'));
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyong1400 2015-05-29 11:51
    已采纳
    $tags = '';
    foreach($news_tag_array as $tag){
       $tags .= '<a href="javascript:;">'.$tag.' </a>';
    }
    echo json_encode(array('returnnews' =>  '<div class="news-item-page">
                                        <h3 class="text-info" style="margin-top:0">'.$latestnews->news_subject.'</h3>
                                        '.$latestnews->news_content.'
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <ul class="list-inline blog-tags">
                                                <li>
                                                    <i class="fa fa-tags"></i>'.$tags.'</li>
                                            </ul>
                                        </div>
                                   </div>'));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanlu0559 2015-05-29 11:48

    Prepare the string first. With all loops which you want. Then put it into array and send it into json_encode(). Get the result.

    $str = '';
    foreach($news_tag_array as $tag){
        $str .= '<a href="javascript:;">';
    }
    echo json_encode(array(
        'returnnews' => '<div ...'.$latestnews.'</div ... '.$str.' ... ',
    ));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题