I have an array of excluded category IDs. I'm trying to fetch data from the DB and filter products based on this array.
- 1000 is an example of Category Level 1 ID
- 1000001 is an example of Category Level 2 ID
- 1000001021 is an example of Category Level 3 ID
So if level 1 is excluded, products of level 2 and 3 are automatically excluded. Currently I'm doing this:
$excludedCategories = ['1000', '100011'];
while ($row = $result->fetch_assoc()) {
$category = "1". $row['category'] . $row['sub_category'] . $row['category_id'];
foreach ($excludeCategories as $exCategory) {
if( substr($category, 0, strlen($exCategory)) === $exCategory ) {
echo 'Category'. $category . " matches " . $exCategory . "<br>";
continue;
}
}
}
.. but is there a better way of doing it without running the second loop?
in_array($category, $excludedCategories)?