Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse the items in an multidimentional array php

Tags:

arrays

php

I am creating an e-commerce website where I have to give the categories that a shop has in a particular array . Currently the data getting retrieved from a mysql table contains the same category id's in different array items if the array has different subcategory , I have to gather the same category id's in the same array and for subcategories create a nested array . The code is on laravel 4.2 . Here is the format of data coming right now ,

"data": [
    {
      "id": 1,
      "name": "Fruits",
      "sub_category_names": [
        "Dairy Whiteners"
      ],
      "total_items": 69
    },
    {
      "id": 1,
      "name": "Fruits",
      "sub_category_names": [
        "Tea & Coffee"
      ],
      "total_items": 69
    },
    {
      "id": 1,
      "name": "Fruits",
      "sub_category_names": [
        "Concentrates - Tang etc"
      ],
      "total_items": 69
    },
    {
      "id": 2,
      "name": "Beverages",
      "sub_category_names": [
        "Tea & Coffee"
      ],
      "total_items": 28
    },
    {
      "id": 2,
      "name": "Beverages",
      "sub_category_names": [
        "Concentrates - Tang etc"
      ],
      "total_items": 28
    }
  ]

Here is what I need ,

"data": [
    {
      "id": 1,
      "name": "Fruits",
      "sub_category_names": [
        "Dairy Whiteners" , "Concentrates - Tang etc" , "Tea & Coffee"
      ],
      "total_items": 69
    } ,

    {
      "id": 2,
      "name": "Beverages",
      "sub_category_names": [
        "Tea & Coffee" , "Concentrates - Tang etc"
      ],
      "total_items": 28
    }
  ]

The code I wrote for the above ,

// For the current categories create a common array for subcategories for same categories
        $filteringSelectedCategories = [];
        $subCategoriesNamesLocal = [];
        $innerIndex = 0;
        for ($i = 0; $i < count($selectedCategories); $i++) {

            // to prevent undefined offset error
            if (!isset($selectedCategories[$i + 1])) {
                continue ;
                // if 6 don't exist then deal with 5
            }
            // if the id of two items is same then push that sub category name in the same array
            if ($selectedCategories[$i]['id'] === $selectedCategories[$i + 1]['id']) {
                array_push($subCategoriesNamesLocal, $selectedCategories[$i]['sub_category_names']);
            } 
            // if the id is different then push the array values with the sub category name in an array 
            else {

                $filteringSelectedCategories[$innerIndex]['id'] = $selectedCategories[$i]['id'];
                $filteringSelectedCategories[$innerIndex]['name'] = $selectedCategories[$i]['name'];
                $filteringSelectedCategories[$innerIndex]['sub_category_names'] = $subCategoriesNamesLocal;
                $filteringSelectedCategories[$innerIndex]['total_items'] = $selectedCategories[$i]['total_items'];

                // nullify the array after assigning the value
                $subCategoriesNamesLocal = [];
                // increment the new array index
                $innerIndex = $innerIndex + 1;

            }
        }

Here is the output I get from the above ,

"data": [
    {
      "id": 1,
      "name": "Fruits",
      "sub_category_names": [
        [
          "Dairy Whiteners"
        ],
        [
          "Tea & Coffee"
        ]
      ],
      "total_items": 69
    }
  ]
like image 641
Siddharth Sharma Avatar asked Mar 01 '26 11:03

Siddharth Sharma


1 Answers

I'm not entirely sure I see what offset error could occur but I believe you could easily get away with;

foreach ($selectedCategories as $key => $data) {
    $newKey = $data['id'];
    $filteringSelectedCategories[$newKey]['id'] = $data['id'];
    $filteringSelectedCategories[$newKey]['name'] = $data['name'];
    $filteringSelectedCategories[$newKey]['sub_category_names'][] = $data['sub_category_names'];
    $filteringSelectedCategories[$newKey]['total_items'] = $data['total_items'];
}
like image 108
glend Avatar answered Mar 04 '26 02:03

glend



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!