PHP:从数组中获取变体

时间:2013-11-18 06:41:01

标签: php arrays foreach while-loop

是否可以使用此数组

 array ( 
        [0] => array ( 
            [0] => 'Size',
            [variation_id] => array ( 
                [0] => 'S',
                [1] => 'M',
                [2] => 'L',
                [3] => 'XL'
            ) 
        ),
        [1] => array ( 
            [0] => 'Length',
            [variation_id] => array ( 
                [0] => '14 Inches',
                [1] => '18 Inches'8
            ) 
        ) 
    )

获得这样的结果

变体名称

  1. 变异(S,14 Inces)
  2. 变体(S,18 Inces)
  3. 变体(M,14 Inces)
  4. 变体(M,18 Inces)
  5. 变体(L,14 Inces)
  6. 变体(L,18 Inces)
  7. 变体(XL,14 Inces)
  8. 变体(XL,18 Inces)
  9. 如果没有,请给我一些建议.. 感谢

1 个答案:

答案 0 :(得分:0)

只需使用两个foreach

$data = array (
    array (
        'Size',
        'variation_id' => array (
            'S',
            'M',
            'L',
            'XL'
        )
    ),
    array (
        'Length',
        'variation_id' => array (
            '14 Inches',
            '18 Inches',
        )
    )
);
$sizes = $data[0]['variation_id'];
$lengths = $data[1]['variation_id'];

$result = array();
foreach($sizes as $size)
    foreach($lengths as $length)
        $result[] = "variation ($size, $length)";

var_dump($result);