willerson
willerson

Reputation: 79

Adding multiple image urls to JSON object

What i am trying to do is make my JSON object the same as an already developed one, these are the displays:

Original:

{
    "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"
}

My Attempt:

{
    "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"
}

The code i have written so far is:

<?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());
    }

}

?>

The issue is when i'm adding images, in the original JSON object, it is displayed between the [ ] square brackets, also in my test above i cannot add multiple images to the JSON object like the original format, any help would be appreciated.

Upvotes: 0

Views: 1143

Answers (2)

Jeff
Jeff

Reputation: 6943

In this line

 $images = "https:... ";

you are overwriting the array $images you've defined just before.
You want to add, so you've got the choice of doing

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

or

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

or already add the strings when creating the array:

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

Upvotes: 3

Jason
Jason

Reputation: 1

After declaring image array, accidently you are over writing it you should assigen image names on different different indexes like $images[0] = "string image name" and so on.. or use array_push method to push entries in images variavle which is an array.

Hope it will work.

Upvotes: 0

Related Questions