多维数组 - 如何从子数组中获取特定值

2021-12-21 00:00:00 multidimensional-array php

我有以下数组结构:

<前>大批([0] => 数组([product_option_id] => 236[option_id] => 14[名称] => Masura S[类型] => 选择[option_value] => 数组([0] => 数组([product_option_value_id] => 33[option_value_id] => 53[名称] => 阿尔伯[价格] =>[价格前缀] => +)[1] => 数组([product_option_value_id] => 35[option_value_id] => 55[名称] => 罗苏[价格] =>[价格前缀] => +))[必需] => 0)[1] => 数组([product_option_id] => 237[option_id] => 15[名称] => Masura M[类型] => 选择[option_value] => 数组([0] => 数组([product_option_value_id] => 34[option_value_id] => 58[名称] => 罗苏[价格] =>[价格前缀] => +))[必需] => 0))

我发现自己在试图显示此数组中的所有 [name] 值时迷失了方向.

我想要做的是根据第一级[name](如[name] => Masura S)用下拉选择填充表单然后第二个下拉选择带有第二级 [name](如 [name] => Alb).

如果您有任何指点,我将不胜感激...

解决方案

您可以通过以下方式填充第一个选择:

<?phpfor ( $i=0; $i <$c; $i++){$c2=count($array[$i]);对于 ($j=0;$j<$c2;$j++){?><option><?php echo $array[$i][$j]['name'];?></option><?php }} ?></选择>

I have the following array structure:

Array
(
    [0] => Array
        (
            [product_option_id] => 236
            [option_id] => 14
            [name] => Masura S
            [type] => select
            [option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 33
                            [option_value_id] => 53
                            [name] => Alb
                            [price] => 
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [product_option_value_id] => 35
                            [option_value_id] => 55
                            [name] => Rosu
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [required] => 0
        )

    [1] => Array
        (
            [product_option_id] => 237
            [option_id] => 15
            [name] => Masura M
            [type] => select
            [option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 34
                            [option_value_id] => 58
                            [name] => Rosu
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [required] => 0
        )
)

I find myself lost in trying to display all the [name] values from this array.

What I am trying to do is to populate a form with dropdown selects based on the first level [name] (like [name] => Masura S) and then a second dropdown select with the second level [name] (like [name] => Alb).

I would appreciate it if you have any pointers...

解决方案

You can populate the first select this way:

<select>

    <?php $c=count($array);
    for ( $i=0; $i < $c; $i++)
    { ?>
        <option><?php echo $array[$i]['name'];?></option>
    <?php } ?>   

</select>

2nd select:

<select>

    <?php 
    for ( $i=0; $i < $c; $i++)
    { 
        $c2=count($array[$i]); 
        for ($j=0;$j<$c2;$j++){ 
    ?>
        <option><?php echo $array[$i][$j]['name'];?></option>
    <?php }} ?>

</select>

相关文章