-1

I was hoping that I would be able to do it, but again I ran into something where I am powerless... Please help me with a solution. After the call I get a JSON Response. I decode the response and read individual fields from "data". But I came across the EAN and PLU fields where the field looks like "ean": ["2112244"] and the processing I'm using reads 0 = false or 1 = true from that.

And I don't know how to read it in order to write the EAN of the given product into the database.

Decode response

$data = json_decode($response, true);
foreach($data["data"] as $row){
        $categoryid = $row["_categoryId"];
        $productid = $row["id"];
        $name = $row["name"];
        $ean = $row["ean"];
        $plu = $row["plu"];
        $externalid = $row["externalId"];
        $units = $row["unit"];
        $unitsmeasurment = $row["unitMeasurement"];
        $packaging = $row["packaging"];
        $packagingmeasurment = $row["packagingMeasurement"];
        }

Json response

"data": [
        {
            "_categoryId": "415874342455239",
            "_cloudId": "349305323",
            "_defaultCourseId": null,
            "_eetSubjectId": null,
            "_supplierId": null,
            "allergens": [],
            "alternativeName": null,
            "created": "2023-01-25T12:39:57.19Z",
            "currency": "CZK",
            "deleted": false,
            "deliveryNoteIds": null,
            "description": null,
            "discountPercent": "0",
            "discountPermitted": true,
            "display": true,
            "ean": ["54544844544"],
            "externalId": null,
            "features": [],
            "flags": "4096",
            "hexColor": "#623320",
            "id": "200915715273883",
            "imageUrl": null,
            "margin": null,
            "marginMin": null,
            "modifiedBy": "411782225143287",
            "name": "Americano",
            "notes": null,
            "onSale": false,
            "packageItem": "1",
            "packaging": "1",
            "packagingMeasurement": "1",
            "packagingPriceWithVat": null,
            "plu": [],
            "points": "0",
            "priceInPoints": null,
            "priceWithVat": "59",
            "priceWithVatB": null,
            "priceWithVatC": null,
            "priceWithVatD": null,
            "priceWithVatE": null,
            "priceWithoutVat": "51.30434782608696",
            "purchasePriceWithoutVat": null,
            "requiresPriceEntry": false,
            "sortOrder": "0",
            "stockDeduct": true,
            "stockOverdraft": "ALLOW",
            "subtitle": null,
            "supplierProductCode": null,
            "tags": null,
            "unit": "Piece",
            "unitMeasurement": "Piece",
            "vat": "1.15",
            "versionDate": "2023-01-25T12:39:57.19Z"
        },
  • 1
    Index it with `$row['ean'][0]` – Barmar Jan 30 '23 at 17:59
  • 2
    Since these are arrays, there could be more than one value in them. How do you want to handle multiple values? – Barmar Jan 30 '23 at 18:00
  • @Barmar Thank you. I tried $row["ean"[]] and it surprisingly didn't work. – Jiří Liška Jan 30 '23 at 18:01
  • I didn't think about multiple values. But there really could be more. In the database, I would like them separated by a comma, i.e. ",". Is it possible to do this? – Jiří Liška Jan 30 '23 at 18:03
  • 2
    Don't do that: https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Barmar Jan 30 '23 at 18:03
  • Why are you surprised? `"ean"[]` is invalid syntax. – Barmar Jan 30 '23 at 18:04
  • Multiple values in the response look like this "ean": [ "2010000210949", "45454544544" ], – Jiří Liška Jan 30 '23 at 18:05
  • Ok, I get it. But I could basically do anything to separate them? So ., /, -, etc. – Jiří Liška Jan 30 '23 at 18:08
  • If you really want to put a comma-separated list, use `implode(',', $row['ean'])` – Barmar Jan 30 '23 at 18:08
  • Many things surprise me. Unfortunately, I don't always understand the PHP documentation. But it's a language I really like and I'm learning it. I am self-taught by creating a project for myself that I want to complete and learn from. – Jiří Liška Jan 30 '23 at 18:09
  • The only time you can use `[]` without a value inside the brackets is when pushing onto an array like `$array[] = $value;`. – Barmar Jan 30 '23 at 18:11
  • `$row['ean']` is an array, so you should be indexing that, not the string `'ean'`. That's why it's `$row['ean'][0]`. – Barmar Jan 30 '23 at 18:12
  • Thanks for the help and thanks for the explanation. I hope I can figure it out now :) – Jiří Liška Jan 30 '23 at 18:20

1 Answers1

0

I don't endorse this in your database schema, but you can create a comma-separated list.

        $ean = implode(',', $row["ean"]);
Barmar
  • 741,623
  • 53
  • 500
  • 612