简体   繁体   中英

Parse the items in an multidimentional array 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
    }
  ]

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'];
}

You can not make an array push directly in $subCategoriesNamesLocal because you are nesting arrays. Make an array outside and then concatenate it to the field.

Try this:

// 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'] = '"' . implode('","', $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;

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM