How do I iterate over a categories field in an entry I'm using the Scout plugin to sync my entry data to an Algolia search index. In the transformer for the scout.php config file, I need to iterate over all of the categories in a category field on each entry.

How would I do this to output the category at each level like so:

"categories": {
    "lvl0": ["Rigging", "Film Lighting"],
    "lvl1": ["Rigging &gt; Rigging Support", "Film Lighting &gt; Stands"],
    "lvl2": ["Rigging &gt; Rigging Support &gt; Stands"]
}


Here is where i'm at with the transformer:

'transformer' =&gt; static function (craft\base\Element $entry) {
    return [
        'entryTitle' =&gt; $entry-&gt;title,
        'id' =&gt; $entry-&gt;id,
        'url' =&gt; $entry-&gt;url,
        'title' =&gt; $entry-&gt;equipmentFriendlyName,
        'manufacturer' =&gt; $entry-&gt;equipmentManufacturer,
        'modelNumber' =&gt; $entry-&gt;equipmentModelNumber,
        'description' =&gt; $entry-&gt;equipmentDescription,
        'categories' =&gt; $entry-&gt;equipmentCategory-&gt;all(),
    ];
}
Try this approach:

&lt;?php
return [
    'transformer' =&gt; function(craft\base\Element $entry) {
        $level1Categories = [];
        $level2Categories = [];
        $level3Categories = [];

        foreach ($entry-&gt;equipmentCategory-&gt;level(1)-&gt;all() as $category) {
            /** @var $category \craft\elements\Category */
            $level1Categories[] = $category-&gt;title;
            $children           = $category-&gt;getChildren()-&gt;level(2)-&gt;all();

            foreach ($children as $child) {
                /** @var $child \craft\elements\Category */
                $crumb      = [
                    $category-&gt;title,
                    $child-&gt;title,
                ];

                $level2Categories[] = implode(' &gt; ', $crumb);

                foreach ($child-&gt;getChildren()-&gt;level(3)-&gt;all() as $subChild) {
                    /** @var $subChild \craft\elements\Category */
                    $crumb      = [
                        $category-&gt;title,
                        $child-&gt;title,
                        $subChild-&gt;title
                    ];

                    $level3Categories[] = implode(' &gt; ', $crumb);
                }
            }
        }

        return [
            'entryTitle'   =&gt; $entry-&gt;title,
            'id'           =&gt; $entry-&gt;id,
            'url'          =&gt; $entry-&gt;url,
            'title'        =&gt; $entry-&gt;equipmentFriendlyName,
            'manufacturer' =&gt; $entry-&gt;equipmentManufacturer,
            'modelNumber'  =&gt; $entry-&gt;equipmentModelNumber,
            'description'  =&gt; $entry-&gt;equipmentDescription,
            'thumbnail'    =&gt; !empty($entry-&gt;equipmentImages-&gt;one()) ? (string)('https://acsweb.com' . $entry-&gt;equipmentImages-&gt;one()-&gt;getUrl()) : '',
            'categories'   =&gt; [
                'lvl0' =&gt; $level1Categories,
                'lvl1' =&gt; $level2Categories,
                'lvl2' =&gt; $level3Categories,

            ],
        ];
    },
];```