Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php数组创建下拉列表_Php_Arrays - Fatal编程技术网

从php数组创建下拉列表

从php数组创建下拉列表,php,arrays,Php,Arrays,我有这样的数组: Array ( [0] => Array( [attribute_name] => Brand [attribute_value] => Lee ) [1] => Array( [attribute_name] => Brand [attribute_value] =>

我有这样的数组:

Array (
    [0] => Array(             
        [attribute_name] => Brand             
        [attribute_value] => Lee         
    )      
    [1] => Array(
         [attribute_name] => Brand  
         [attribute_value] => Levis         
    )      
    [2] => Array( 
        [attribute_name] => Brand    
        [attribute_value] => Raymond         
    )      
    [3] => Array( 
        [attribute_name] => Fabric 
        [attribute_value] => Cotton         
    )   
    [4] => Array(
        [attribute_name] => Fabric  
        [attribute_value] => Linen         
    )  
)
// grouping
$grouped = array();
foreach($array as $values) {
    $grouped[$values['attribute_name']][] = $values['attribute_value'];
}
我想从这个数组中创建两个下拉列表,一个用于
品牌
,应该有三个选项,另一个用于
面料
,应该有两个选项

我可以简单地通过检查
属性\u name
品牌
还是
面料
,但这不是静态的,可以用任何东西代替
品牌
面料

我试过很多东西,但都不管用。请帮我做这件事。提前谢谢

$filteredArray = array();
foreach ($array as $key => $value) {
    if ($key === 'Brand' || $key === 'Fabric') {
        array_push($filteredArray, $value);
    }
}

$filteredArray现在只包含品牌和结构。

对于这种结构,必须首先从数据中创建分组数组。因此,反过来,他们将更容易管理。首先,使用一个循环,使用属性名作为键,按下值,对所有这些属性进行分组。将看起来像这样:

Array (
    [0] => Array(             
        [attribute_name] => Brand             
        [attribute_value] => Lee         
    )      
    [1] => Array(
         [attribute_name] => Brand  
         [attribute_value] => Levis         
    )      
    [2] => Array( 
        [attribute_name] => Brand    
        [attribute_value] => Raymond         
    )      
    [3] => Array( 
        [attribute_name] => Fabric 
        [attribute_value] => Cotton         
    )   
    [4] => Array(
        [attribute_name] => Fabric  
        [attribute_value] => Linen         
    )  
)
// grouping
$grouped = array();
foreach($array as $values) {
    $grouped[$values['attribute_name']][] = $values['attribute_value'];
}
此循环将创建这样的结构:

Array
(
    [Brand] => Array
        (
            [0] => Lee
            [1] => Levis
            [2] => Raymond
        )

    [Fabric] => Array
        (
            [0] => Cotton
            [1] => Linen
        )

)
分组之后。然后是演示:

将此作为一个想法:

<form method="POST">
<?php foreach($grouped as $label => $values): ?>
    <label><?php echo $label; ?></label>
    <select name="select_values[<?php echo $label; ?>]">
        <?php foreach($values as $value): ?>
        <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
        <?php endforeach; ?>
    </select><br/><br/>
<?php endforeach; ?>
<br/><input type="submit" />
</form>

在给出答案之前,请先理解问题$钥匙会给我0 1 2 3 4不是品牌和面料