PHP从嵌套数组输出密钥名称

I have an array:

$cats = array(
  'guitars' => array (
    'electric-guitars',
    'acoustic-guitars',
    'bass',
    'electro-acoustic',
    'effects-fx-pedals',
    'guitar-amps',
    'classical-guitars',
    'guitar-recording',
    'folk',
    'cases-stands',
    'guitar-care',
    'guitar-accessories'
  ),
  'drums',
  'keyboards',
  'studio',
  'computer',
  'dj-pa',
  'brass',
  'accessories'
);

And I'm trying to output the top level keys so it outputs the following:

guitars
drums
keyboards
studio
computer
dj-pa
brass
accessories

I'm using this foreach loop:

foreach($cats as $cat => $cat_name) {
  echo '<div>' . $cat_name . '</div>';
}

But this instead outputs:

Array
drums
keyboards
studio
computer
dj-pa
brass
accessories

If anyone can help it'd be greatly appreciated!

You're trying to output the value, rather than the key. When you use foreach(... as ... => ...), the syntax is foreach($array as $key => $value). Your variable names are backwards. Also, you need to use is_array() to figure out what the output is, because you are inconsistent in your use of key names versus values:

foreach($cats as $cat_name => $cat) {
    if(is_array($cat)) { // guitars
        echo '<div>' . $cat_name . '</div>';
    }
    else { // the other values have numeric keys
        echo '<div>' . $cat . '</div>';
    }
}

Output (tags omitted for readability):

guitars
drums
keyboards
studio
computer
dj-pa
brass
accessories