簡體   English   中英

解析多維數組php中的項目

[英]Parse the items in an multidimentional array php

我正在創建一個電子商務網站,我必須提供商店在特定陣列中的類別。 目前,從mysql表中檢索的數據在不同的數組項中包含相同的類別ID,如果數組具有不同的子類別,我必須在同一數組中收集相同的類別ID,並且子類別創建嵌套數組。 代碼在laravel 4.2上。 這是現在的數據格式,

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

這就是我需要的,

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

我為上面寫的代碼,

// 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;

            }
        }

這是我從上面得到的輸出,

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

我不完全確定我會看到可能發生的偏移誤差,但我相信你可以很容易地逃脫;

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

您無法直接在$ subCategoriesNamesLocal中進行數組推送,因為您正在嵌套數組。 在外面創建一個數組,然后將其連接到字段。

嘗試這個:

// 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;

            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM