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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6320681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:59:37  来源:igfitidea点击:

Multidimensional array - how to get specific values from sub-array

phpmultidimensional-array

提问by RePho

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.

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

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).

我所试图做的是填充基于第一级下拉列表中选择一种形式[name](如[name] => Masura S),然后第二个下拉与第二级选择[name](像[name] => Alb)。

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

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

采纳答案by Belonwu Ogugua

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>

回答by Usman Ahmed

Try this:

尝试这个:

$name = array_column($array, 'name');

回答by geekuality

I'd separate the names to separate arrays like this, after that it should be easy to populate dropdowns as needed:

我会像这样将名称分开以分隔数组,之后应该很容易根据需要填充下拉列表:

$product_names = $option_names = array();
foreach ($products as $index => $product) {
    $product_names[$index] = $product['name'];

    foreach ($product['option_value'] as $option) {
        $option_names[$index][] = $option['name'];
    }
}

When you want product name for array index 0, you'd use $product_names[0] (a string) and option names for that product could be found from $option_names[0] (an array).

当您想要数组索引 0 的产品名称时,您可以使用 $product_names[0](一个字符串)并且可以从 $option_names[0](一个数组)中找到该产品的选项名称。

Code above doesn't care about existing ID's so if you need them for the form, you'd need to expand the code a bit more.

上面的代码不关心现有的 ID,所以如果你需要它们用于表单,你需要更多地扩展代码。

回答by Ashraf Kaabi

You will need to use a recursive function
here's an example

您将需要使用递归函数,
这是一个示例

function recursion($array) {
    foreach ($array as $key => $value) {
        echo $value;
        if (is_array($value))
            $this->recursion($value);
    }
}

recursion($array);