如何获取数组的键作为标题

时间:2014-01-14 09:51:57

标签: php arrays foreach

这是循环中的数组

(
    [Size] => Array
        (
            [07601-01] => S,
            [07601-02] => M,
            [07601-03] => L,
            [07601-04] => XL,
            [07601-05] => XXL,
        )

    [other] => Array
        (
            [07601-01] => S,
            [07601-02] => M,
            [07601-03] => L,
            [07601-04] => XL,
            [07601-05] => XXL,
        )

)

这是我的循环

<?php 
     foreach($product_atttribute as $attr) { 
?>

尺寸

  1. 取值
  2. 中号
  3. XL
  4. 其他 .....等

    <?php 
      } 
    ?>
    

    在上面的循环中,如何将Sizeother等作为如下标题获取

3 个答案:

答案 0 :(得分:3)

foreach($product_attribute as $key => $value) { }

答案 1 :(得分:2)

如果需要,请使用array_keys检索已传递数组的所有键

答案 2 :(得分:0)

$arr = array(
    'Size' => array
        (
            '07601-01' => 'S',
            '07601-02' => 'M',
            '07601-03' => 'L',
            '07601-04' => 'XL',
            '07601-05' => 'XXL',
        ),

    'other' => array
        (
            '07601-01' => 'S',
            '07601-02' => 'M',
            '07601-03' => 'L',
            '07601-04' => 'XL',
            '07601-05' => 'XXL',
        )

);

foreach( $arr as $key => $value ) {
    echo '<strong>', $key, '</strong><br/>';
    $num = 1;
    foreach( $value as $size ) {
        echo ' ', $num++, '. ',  $size, '<br/>';
    }
}

输出:

Size
 1. S
 2. M
 3. L
 4. XL
 5. XXL
other
 1. S
 2. M
 3. L
 4. XL
 5. XXL