在PHP

时间:2016-08-01 07:03:33

标签: php arrays

我有来自MYSQL的数据,我使用WHILE循环来获取我想要的所有行。

while($row_metro_address_country_name = $res_metro_address_country_name->fetchRow()) { 


        $notice['data'][] = array("more_images" => "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"".$row_metro_address_country_name[0]."\"></div>");

                    }

我有一个已经存在的数组对象。如何将上面的MYSQL数组结果添加到这个数据中。

$notice['data'][]  = array("by" => author($row_metro_company_doc_legal[1]),
                "property_price"=>$property_price,
                "nickname"=>parse($row_metro_company_doc_legal[5]),
                "more_images"=>$fimg);

结果我得到了

{
    "err_code": 0,
    "err_msg": "success",
    "data": [
        {
            "more_images": "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/jspringext.jpg\"><\/div>"
        },
        {
            "more_images": "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/tspring2.jpg\"><\/div>"
        },
        {
            "by": "author",
            "price": "250,000",
            "nickname": "Spring Valley",
            "more_images": "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/jspringext.jpg\"><\/div>"
}

而不是

{
    "err_code": 0,
    "err_msg": "success",
    "data": [
        {
            "by": "author",
            "price": "250,000",
            "nickname": "Spring Valley",
            "more_images": "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/jspringext.jpg\"><\/div><div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/jspringext.jpg\"><\/div><div class=\"swiper-slide\"><img class=\"avatar\" src=\"http:\/\/apa.com\/wp-content\/uploads\/2015\/07\/tspring2.jpg\"><\/div>"

}

2 个答案:

答案 0 :(得分:0)

您应该能够像这样附加到对象

$notice['data']["more_images"] .= "<div class=\"swiper-slide\"><img class=\"avatar\" src=\"".$row_metro_address_country_name[0]."\"></div>";

虽然,你是否会在循环中返回多行[&#34;数据&#34;]?

答案 1 :(得分:0)

$notice['data'][]  = array("by" => author($row_metro_company_doc_legal[1]),
                "property_price"=>$property_price,
                "nickname"=>parse($row_metro_company_doc_legal[5]),
                "more_images"=>$fimg);

将其替换为

$notice['data'][]  = array("by" => author($row_metro_company_doc_legal[1]),
                "property_price"=>$property_price,
                "nickname"=>parse($row_metro_company_doc_legal[5])),
                "more_images"=>array_pop($fimg));