1

I have the following json file with products details:

  "products": [
    {
      "sku": 123,
      "name": "iphone 7",
      "categoryPath": [
        {
          "id": "abcat0800000",
          "name": "Cell Phones"
        },
        {

          "id": "pcmcat209400050001",
          "name": "All Cell Phones with Plans"

        }
      ],
}
]

I would like only to store the last value (ID and NAME) of the categoryPath Array:

          "id": "pcmcat209400050001",
          "name": "All Cell Phones with Plans"

My current code takes the json file, decode the json and insert in products table the information.

    $json = File::get("/json/cell-0.json");
    $data = json_decode($json);
    $array1 = (array)$data;
    //table products
    foreach ($array1['products'] as $obj) {
        DB::table('products')->insert(array(
            'productSku' => ((isset($obj->sku) ? $obj->sku : 1)),
            'productName' => ((isset($obj->name) ? $obj->name : null)),
            'categoryId' => end($obj->categoryPath->id),
            'categoryName' => end($obj->categoryPath->name)
        ));

Taking into consideration that array->categoryPath have multiple fields I would like to use a function (eg: end()) in order to take id and name only of the last values.

Using end($obj->categoryPath->id) I receive the following error ->

Attempt to modify property of non-object

Is this the best way to retrieve the last value of a multidimensional array?

2 Answers 2

1

You could use end() probably but your accessors would have to be outside the end() call (untested):

    foreach ($array1['products'] as $obj) {

    DB::table('products')->insert(array(
        'productSku' => ((isset($obj->sku) ? $obj->sku : 1)),
        'productName' => ((isset($obj->name) ? $obj->name : null)),
        'categoryId' => end($obj->categoryPath)->id,
        'categoryName' => end($obj->categoryPath)->name
    ));
Sign up to request clarification or add additional context in comments.

Comments

0

The way you're getting the last element is incorrect, here is the refactored code. I also eliminated the need to cast data as an array as well.

$json = File::get("/json/cell-0.json");
$data = json_decode($json, true);
//table products
foreach ($data['products'] as $product) {
    $lastCategory = isset($product['categoryPath']) && $size = sizeof($product['categoryPath']) ? $product['categoryPath'][$size-1] : array('id' => null, 'name' => null);
    DB::table('products')->insert(
        array(
            'productSku' => isset($product['sku']) ? $product['sku'] : 1,
            'productName' => isset($product['name']) ? $product['name'] : null,
            'categoryId' => lastCategory['id'],
            'categoryName' => lastCategory['name']
        )
    );
}

1 Comment

I tried the example. $size is not detected in the second part of $lastCategory. the other option was simpler. thank you so much @Augwa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.