Trouble filtering content output within a snippet

I’m trying to output content to a custom XML file and I need to filter the content one more time but I’m not sure how to do this.

I have an array of strings like this:
<practices>bf82cb61||f82e0c12||328d7ce8c</practices>

Each of these tv selections corelate to a resource with the same link_attribute and a snippet is in place to generate their names from the resource’s pagetitle.

<?php
$output = '';
$parts = explode("||", $input);
foreach($parts as $serviceId){
    $where = array(
        'link_attributes' => $serviceId,
        'template:IN' => [10,20]
    );
    $item = $modx->getObject('modResource', $where);
    if(($item)){
        $res = $item->toArray();
        $pagetitle = htmlentities($res['pagetitle']);
        $output .= '<serviceIndustry>' . $pagetitle . ' </serviceIndustry>';
    }
}
return $output;

I need to filter these results by an additional radio TV within each resource. Each resourse can be either a service, an industry or a landing page. I need to only include services & industries.

Any ideas?

Thanks

    if ($item){
        $tv = $item->getTVValue('name_of_tv');
        if (!in_array($tv, ['service', 'industry'], true)) {
            continue; // this continues the foreach loop to the next serviceId
        }
        $res = $item->toArray();
        $pagetitle = htmlentities($res['pagetitle']);
        $output .= '<serviceIndustry>' . $pagetitle . ' </serviceIndustry>';
    }

If you’re running this code a lot (say > 100 times) in an uncached call, you’ll likely want to optimise it as you’re otherwise going to send a lot of queries to the database. Perhaps caching a lookup array across requests (eg some map like ['bf82cb61' => 'Page Title if it's a service or industry'] you read into memory once per request) .

ARHG!!! I just started writing and i was soo close! Thank you!!

1 Like