[Solved] Check if array has [0][1][2] or just single item


You can test whether CustomCategory contains is an indexed or associative array by checking for an element with index 0. If not, you can wrap the contents in an array and then do your foreach loop.

$customCategory = $obj["Store"]["CustomCategories"]["CustomCategory"];
if (!$customCategory[0]) {
    $customCategory = array($customCategory);
}
foreach ($customCategory as $category => $val) {
    ...
}

0

solved Check if array has [0][1][2] or just single item