猿问

如何在 PHP 中使用对象数组以及如何获得不同的产品类别和子类别?

我有一个 Product 对象列表,定义如下:


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;

    }


}

每个 Product 对象都有一个类别和子类别对象作为定义的一部分,它们的定义如下:


产品分类:


class ProductCategory{

    public $name;

    public $id;


    public function __construct($categoryName, $categoryId){

        $this->name = $categoryName;

        $this->id = $categoryId;

    }

}

产品子类别:


class ProductSubCategory{

    public $name;

    public $id;


    public function __construct($subCategoryName, $subCategoryId){

        $this->name = $subCategoryName;

        $this->id = $subCategoryId;

    }

}

例如,父类别 Non-Medicated 具有以下子类别(父类别、子类别):


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

我试图通过迭代产品来返回作为主要类别的对象数组以及每个主要类别的不同子类别,以便最终获得如下显示:

  • 非药物

    • 玻璃

    • 配件

    • 润唇膏

    • 洗剂

    • 馏出物

    • 墨盒

    • 酊剂

    • 胶囊

到目前为止我所做的:

到目前为止,我可以通过这样做获得类别:

$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 );

其中 $allProducts 是上面定义的 Product 对象数组。


我想做的是在这里得到一些东西 $categories[0][0] 是主要类别,然后 $categories[0][1] 是子类别 1,$categories[0][2] 是子类别 2,等等。


我也愿意以另一种方式接近它——这是我能想到的最简单的方法。任何关于如何更好地构建它的建议也将不胜感激。我以前听说过 Collections,但我从未使用过它们,我不确定它们是否适用于这里。


守着一只汪
浏览 158回答 3
3回答

墨色风雨

阅读您的问题看起来更多 ProductSubCategory 属于 ProductCategory 对象而不是 Product 对象。基本上,我相信你可以通过Product一个ProductCategory对象和ProductCategory一个数组来更好地设计它ProductSubCategory。因此,当您获得Product对象时,您可以访问它ProductCategory和所有ProductSubCategory对象。所以,你可以像这样迭代你的对象:foreach($allProducts as $product) {    $category = $product->getCategory(); // Get your object category    $subCategories = $category->getSubCategories(); // Get your sub categories from CategoryObject    echo "Category: " . $category;    foreach($subCategories as $subCategory) {        echo "Sub category: " . $subCategory;    }}您可以在上面的问题中得到您期望的结果。

摇曳的蔷薇

您可以在一个循环中执行此操作:$allCategories = [];foreach($allProducts as $product){    if(!isset($allCategories[$product->category)) // add the category        $allCategories[$product->category] = [];       if(!isset($allCategories[$product->category][$product->subcategory])) // add the subcategory to top-level category        $allCategories[$product->category][] = $product->subcategory;}

拉风的咖菲猫

我假设有一个函数可以获取给定类别的所有子类别$categories = 数组();$allProducts = $this->get_all_products();foreach ( $allProducts as $key => $item ) {    $categories[$key][] = $item->category;    foreach ($this->get_all_subcategories($item->category) as $subCat) { //假设有一个 get_all_subcategories 函数可用,它可以是一个以 category 为 key 和 subcategories 为 value 的数组        $categories[$key][] = $subCat;    }}打印_r($类别);修改$categories = 数组();$allProducts = $this->get_all_products();foreach ( $allProducts as $key => $item ) {    $categories[$key][0] = $item->category;    if (!in_array($item->subcategory, $categories[$key]))        $categories[$key][] = $item->subcategory;}打印_r($类别);
随时随地看视频慕课网APP
我要回答