for($h=1;$h<100;$h++) { if(!array_key_exists($h, $categories)) { break; } if (!array_key_exists(1, $categories[$h])) { break; } for($i=1;$i<100;$i++) { if(!array_key_exists($i, $categories[$h])) { break; } //print(sizeof($strings)); // This will turn the array into a string with all subcategories, separated by ;. // For example, 0;3;53 if subcategory 0, 3 and 53 are checked $strings[$h][$i] = implode(';', $categories[$h][$i]); } }