0

I'm doing a REST api with Lumen framework.

One api call give me all my categories of product :

{
    "categories": [
        {
            "id": 1,
            "name": "Boissons",
            "parent": null,
            "categories": [
                {
                    "id": 3,
                    "name": "Sans alcool",
                    "parent": 1,
                    "categories": []
                },
                {
                    "id": 4,
                    "name": "Bières",
                    "parent": 1,
                    "categories": []
                },
                {
                    "id": 5,
                    "name": "Spiritueux",
                    "parent": 1,
                    "categories": [
                        {
                            "id": 6,
                            "name": "Rhums",
                            "parent": 5,
                            "categories": []
                        },
                        {
                            "id": 7,
                            "name": "Whiskys",
                            "parent": 5,
                            "categories": []
                        }
                    ]
                }
            ]
        },
        {
            "id": 2,
            "name": "Gastronomie",
            "parent": null,
            "categories": []
        }
    ]
}

I want to delete categories that don't contain any product.

I do :

$models = $this->removeCategoriesWithoutProduct($company, $models);

Where the method removeCategoriesWithoutProduct is describe below :

private function removeCategoriesWithoutProduct($company, $models)
{
    for ($i = count($models)- 1; $i >= 0; $i--) {
        // get number of product in category
        $count = $this->productRepository->countCompanyCategory($company, $models[$i]->id);

        if ($count == 0 && count($models[$i]->categories) == 0) {
            Log::debug('No product and no child categories', ['model' => $models[$i]->name]);
            $models[$i] = null;
        } else {
            // check child categories
            $models[$i]->categories = $this->removeCategoriesWithoutProduct($company, $models[$i]->categories);

            // if category without product and no one child contain product
            // delete the parent category (the current one)
            if ($count == 0) {
                $delete = true;
                foreach ($models[$i]->categories as $cat) {
                    $delete = $cat == null;
                    if (!$delete) {
                        break;
                    }
                }

                if ($delete) {
                    Log::debug('No product and all child categories empty', ['model' => $models[$i]->name]);
                    $models[$i] = null;
                }
            }
        }
    }

    return $models;
}

And then I get this result :

{
    "categories": [
        {
            "id": 1,
            "name": "Boissons",
            "parent": null,
            "categories": [
                null,
                null,
                {
                    "id": 5,
                    "name": "Spiritueux",
                    "parent": 1,
                    "categories": [
                        null,
                        {
                            "id": 7,
                            "name": "Whiskys",
                            "parent": 5,
                            "categories": []
                        }
                    ]
                }
            ]
        },
        null
    ]
}

If I use unset instead of assign null value I get this result :

{
    "categories": [
        {
            "id": 1,
            "name": "Boissons",
            "parent": null,
            "categories": {
                "2": {
                    "id": 5,
                    "name": "Spiritueux",
                    "parent": 1,
                    "categories": {
                        "1": {
                            "id": 7,
                            "name": "Whiskys",
                            "parent": 5,
                            "categories": []
                        }
                    }
                }
            }
        }
    ]
}

I don't understand why my sub array is fucked up ? I want this result :

{
    "categories": [
        {
            "id": 1,
            "name": "Boissons",
            "parent": null,
            "categories": [
                {
                    "id": 5,
                    "name": "Spiritueux",
                    "parent": 1,
                    "categories": [
                        {
                            "id": 7,
                            "name": "Whiskys",
                            "parent": 5,
                            "categories": []
                        }
                    ]
                }
            ]
        }
    ]
}
Bosshoss
  • 783
  • 6
  • 24
  • Can you [edit] to include an example of the _desired_ output? I'm not really clear what's wrong with the `unset` output. (You might want to edit out the swearing while you're at it, some people will be put off by that kind of thing.) – IMSoP Mar 18 '22 at 10:09
  • Also, you have some debug lines in there; did they show you anything useful? – IMSoP Mar 18 '22 at 10:11
  • 1
    I think you need to reindex the arrays that have been modified, so that they don't contain gaps. – Gras Double Mar 18 '22 at 10:11
  • I edit with the desired result. My code is correct because I remove (set null) to categories that I don't want. When I use array_values `$models = array_values($models);` I get "array_values() expects parameter 1 to be array, object given" – Bosshoss Mar 18 '22 at 10:15
  • 1
    Probably it isn't a regular array, but a Laravel collection. Try this: `$models->values()`. – Gras Double Mar 18 '22 at 10:23
  • Thanks ! `return array_values($models->toArray());` was the solution – Bosshoss Mar 18 '22 at 11:33

0 Answers0