I have a list of Product objects, defined like so:
class Product{
public $description;
public $pricing;
public $name;
public $imageUrl;
public $id;
public $category;
public $subcategory;
public function __construct($id, $description, $pricing, $name, $imageUrl, ProductCategory $category, ProductSubCategory $subcategory){
$this->description = $description;
$this->pricing = $pricing;
$this->name = $name;
$this->imageUrl = $imageUrl;
$this->id = $id;
$this->category = $category;
$this->subcategory = $subcategory;
}
}
Each Product object has a category and subcategory object as part of the definition, these are defined as so:
ProductCategory:
class ProductCategory{
public $name;
public $id;
public function __construct($categoryName, $categoryId){
$this->name = $categoryName;
$this->id = $categoryId;
}
}
ProductSubCategory:
class ProductSubCategory{
public $name;
public $id;
public function __construct($subCategoryName, $subCategoryId){
$this->name = $subCategoryName;
$this->id = $subCategoryId;
}
}
For example, the parent category Non-Medicated has the following subcategories (parent category, subcategory):
Non Medicated, Glass
Non Medicated, Accesories
Non Medicated, Lip Balm
Non Medicated, Lotion
Non Medicated, Distillate
Non Medicated, Cartridges
Non Medicated, Tincture
Non Medicated, Capsules
I am trying to return an array of objects that is the main category as well as the distinct subcategories for each main category by iterating through the products, so that I can ultimately get a display like this:
- Non-Medicated
- Glass
- Accessories
- Lip Balm
- Lotion
- Distillate
- Cartridges
- Tincture
- Capsules
What I've done so far:
So far I can get the categories by doing this:
$categories = array();
$allProducts = $this->get_all_products();
foreach ( $allProducts as $item ) {
if ( ! in_array( $item->category, $categories ) ) {
array_push( $categories, $item->category );
}
}
print_r( $categories );
Where $allProducts is an array of Product objects, as defined above.
So I have this array:
Array ( [0] => ProductCategory Object ( [name] => Non Medicated [id] => 6 ) [1] => ProductCategory Object ( [name] => Infused Edible [id] => 4 ) [2] => ProductCategory Object ( [name] => Concentrate [id] => 2 ) [3] => ProductCategory Object ( [name] => Cartridges / Pens [id] => 3 ) [4] => ProductCategory Object ( [name] => Flower [id] => 1 ) )
What I am trying to do is get something here $categories[0][0] is the main category and then $categories[0][1] is subcategory 1, $categories[0][2] is subcategory 2, etc.
I am also open to approaching it another way - that's just the easiest I could think of off the top of my head. Any advice on how to better structure this would be appreciated as well. I have heard of Collections before, but I've never used them, and I'm not sure if they would be applicable here.