-8

I created a function that gets the product_id and its category_id where the product_id = the product_id supplied like so:

public function getCategorys($product_id){
        $sql = "SELECT category_id,product_id FROM oc_product_to_category WHERE product_id = '$product_id'";
        $result = $this->query($sql);
        $categories = $this->getResult($result);
        return $categories;
    }

This is function is called with the following code:

foreach($product_ids as $product_id){
    $categories[] = $data->getCategorys($product_id);
}

I then create an array containing the product_id and each category_id that product is found in, I did this with the following code:

$product_categories = array();

foreach($categories as $category){

    foreach($category as $cat){
        if($cat['product_id']) 
            $product_categories[] = array(
                'product_id' => $cat['product_id'],
                'category_id' => $cat['category_id']
            );
        }
}

Now when I print_r($product_categories) I get the following result (this is just part of it I wont put the full array in because its large):

Array
(
    [0] => Array
        (
            [product_id] => 9319
            [category_id] => 293
        )

    [1] => Array
        (
            [product_id] => 9319
            [category_id] => 313
        )

    [2] => Array
        (
            [product_id] => 11969
            [category_id] => 395
        )

    [3] => Array
        (
            [product_id] => 11969
            [category_id] => 396
        )

Now what I'm trying to achieve is: I want an array that has the product ID and each category_id that it is found in so for example the first two arrays with product id 9319 and 11969 I would want that to look like this:

[0] => Array
        (
            [product_id] => 9319
            [category_id] => 293,313
        )

    [1] => Array
        (
            [product_id] => 11969
            [category_id] => 395,396
        )

I know this isn't articulated very well but I'm not sure how else to phrase this question! I essentially want all the duplicate product id's removed and all their category_id's as one comma separated string in the same array.

EDIT:

I tried the answer provided Group array by subarray values

However I'm getting this output instead now:

Array
(
    [22] => Array
        (
            [133] => Array
                (
                    [product_id] => 22
                    [category_id] => 313
                )

        )

    [59] => Array
        (
            [320] => Array
                (
                    [product_id] => 59
                    [category_id] => 294
                )

            [321] => Array
                (
                    [product_id] => 59
                    [category_id] => 331
                )

        )

    [63] => Array
        (
            [368] => Array
                (
                    [product_id] => 63
                    [category_id] => 292
                )

            [369] => Array
                (
                    [product_id] => 63
                    [category_id] => 302
                )

        )

    [87] => Array
        (
            [51] => Array
                (
                    [product_id] => 87
                    [category_id] => 293
                )

            [52] => Array
                (
                    [product_id] => 87
                    [category_id] => 313
                )

        )

This is nearly close to what I want but I want all the category ids in array for that product_id.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike Abineri
  • 409
  • 2
  • 13
  • Does this answer your question? [Group array by subarray values](https://stackoverflow.com/questions/7574857/group-array-by-subarray-values) – Jeto Nov 27 '19 at 10:42
  • 1
    Would you not be better off with an array that has the `product_id` as the key which contained an array of `category_id`'s Comma delimited lists mark the entry point to the road to hell – RiggsFolly Nov 27 '19 at 10:42

1 Answers1

4

Would you not be better off with an array that has the product_id as the key which contained an array of category_id's Comma delimited lists mark the entry point to the road to hell

Make you foreach loop do this instead

$product_categories = [];

foreach($categories as $cat){
    $product_categories[$cat['product_id']][] = $cat['category_id'];
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149