Parsing elements in a multidimensional php array
I am creating an e-commerce site where I have to specify the categories that the store has in a specific array. Currently the data retrieved from mysql table contains the same category ID in different array elements, if the array has a different subcategory, I need to collect the same category ID in the same array, and create a nested array for the subcategories. The code is on laravel 4.2. Here is the format of the data coming in 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
}
]
This 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 above is
// 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 result I get from the above,
"data": [
{
"id": 1,
"name": "Fruits",
"sub_category_names": [
[
"Dairy Whiteners"
],
[
"Tea & Coffee"
]
],
"total_items": 69
}
]
+3
source to share
3 answers
I'm not really sure I can see an offset error can occur, but I believe you can easily escape.
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'];
}
+2
source to share
Try the following:
// 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;
}
}
0
source to share