将多个图片网址添加到JSON对象

时间:2018-08-04 22:21:51

标签: php

我想要做的是使我的JSON对象与已经开发的对象相同,这些是显示内容:

原始

{
    "name": "test product",
    "descriptionUrl": "https:\/\/www.site.club\/",
    "images": [
    "https:\/\/thesite.com\/kf\/HTB1bGBljyAKL1JjSZFoq6ygCFXa7\/TY-Unicorn-Big-Eyes-Plush-Toys-Ty-Beanie-Boos-Kids-Lovely-Children-Gifts-Kawaii-Stuffed-Animals.jpg",
    "https:\/\/thesite.com\/kf\/HTB1rCK3bfJNTKJjSspoq6A6mpXaJ\/TY-Unicorn-Big-Eyes-Plush-Toys-Ty-Beanie-Boos-Kids-Lovely-Children-Gifts-Kawaii-Stuffed-Animals.jpg",
    "https:\/\/thesite.com\/kf\/HTB1zWO2eGmWQ1JjSZPhq6xCJFXaa\/TY-Unicorn-Big-Eyes-Plush-Toys-Ty-Beanie-Boos-Kids-Lovely-Children-Gifts-Kawaii-Stuffed-Animals.jpg",
    "https:\/\/thesite.com\/kf\/HTB13sOWXoRIWKJjSZFgq6zoxXXah\/TY-Unicorn-Big-Eyes-Plush-Toys-Ty-Beanie-Boos-Kids-Lovely-Children-Gifts-Kawaii-Stuffed-Animals.jpg"
    ],
    "priceRange": {
        "minPrice": "19.99",
        "maxPrice": "19.99",
        "currency": "USD"
    },
    "descriptionHtml": "HTML code can potentially go here!",
    "descriptionText": "Test product description"
}

我的尝试:

{
    "name": "test product",
    "descriptionUrl": "https:\/\/www.site.club\/",
    "images": "https:\/\/www.site.club\/images\/img-instagram-icon.png",
    "priceRange": {
        "minPrice": "19.99",
        "maxPrice": "19.99",
        "currency": "USD"
    },
    "descriptionHtml": "HTML code can potentially go here!",
    "descriptionText": "Test product description"
}

到目前为止我写的代码是:

<?php

if (isset($_POST['submitNewProduct'])) {

    // TRY/CATCH //
    try {

        // 1 - PRICE ARRAY //
        $prices = [];
        foreach (['minPrice', 'maxPrice'] as $searchField) {
            $prices[$searchField] = $_POST['product_price'];
        }   
        $prices['currency'] = 'USD';

        // 2 - IMAGES ARRAY //
        $images = [];
        $images = "https://www.site.club/images/img-header-39847.png";
        $images = "https://www.site.club/images/img-instagram-icon.png";

        // SETUP THE JSON OBJECT //     
        $productData = array('name' => $_POST['product_name'], 
                             'descriptionUrl' => getUrl(), 
                             'images' => $images, 
                             'priceRange' => $prices, 
                             'descriptionHtml' => 'HTML code can potentially go here!', 
                             'descriptionText' => $_POST['product_description']
                             );

        print_r($productData);

        // INSERTION //
        $i = DB::getInstance()->insert(
            'products',
        [
            'product_unique_id' => generateId("wlu", $member),
            'product_category_id' => 0,
            'product_name' => $_POST['product_name'],
            'product_json_body' => json_encode($productData, JSON_PRETTY_PRINT),
            'product_url' => getUrl(),
            'product_active' => 'Y',
            'product_date' => date('Y-m-d H:i:s')
        ]); 

        stdmsg("..."); 

    } catch (Exception $e) {
        stderr($e->getMessage());
    }

}

?>

问题是当我在原始JSON对象中添加图像时,它显示在[]方括号之间,同样在上述测试中,我无法像原始格式一样向JSON对象添加多个图像,没有任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在此行

 $images = "https:... ";

您将覆盖之前定义的数组$images
您要添加,因此可以选择

$images[] = "https:....";

array_push($images, "https://www...");

或在创建数组时已添加字符串:

// images = []; // not needed then!
$images = ["https:..firstimage..", "https:secondimage"];

答案 1 :(得分:0)

在声明图像数组后,您意外地覆盖了它,您应该在不同的不同索引上使用图像名称,例如  $ images [0] =“字符串图像名称”,依此类推。 或使用array_push方法在图像可变数组中推送条目。

希望它会起作用。