没有从数组中获得所需的输出

时间:2017-11-17 13:56:51

标签: php arrays multidimensional-array

我需要创建一个动态数组,但无法正确完成。 我需要这样的东西:

产品名称

    • 黑色
    • 红色
    • 绿色

我想出了这段代码,它产生了上面的文字,所以我的逻辑必须是正确的。

$set = array();
$set['name'] = "Product Name";

$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");

echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
    echo "<pre>- $o</pre>";
    $set['options'][]['name'] = $o;

    foreach ($values[$o] as $v) {
        echo "<pre>-- $v</pre>";
        $set['options'][]['values']['name'] = $v;
    }
}

使用上述代码创建的数组是:

Array
(
  [name] => Product Name
  [options] => Array
  (
    [0] => Array
    (
     [name] => top
    )
[1] => Array
                (
                    [values] => Array
                        (
                            [name] => White
                        )

                )

            [2] => Array
                (
                    [values] => Array
                        (
                            [name] => Black
                        )

                )

            [3] => Array
                (
                    [name] => bottom
                )

            [4] => Array
                (
                    [values] => Array
                        (
                            [name] => Red
                        )

                )

            [5] => Array
                (
                    [values] => Array
                        (
                            [name] => Green
                        )
                )
        )
)

我想要的输出是:

Array
(
    [name] => Product Name
    [options] => Array
        (
            [0] => Array
                (
                    [name] => top
                    [values] => Array
                        (
                            [0] => Array
                                (
                                    [name] => White
                                )

                            [1] => Array
                                (
                                    [name] => Black
                                )

                        )

                )

            [1] => Array
                (
                    [name] => bottom
                    [values] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Red
                                )

                            [1] => Array
                                (
                                    [name] => Green
                                )

                        )

                )

        )

)

我错过了什么?

1 个答案:

答案 0 :(得分:2)

你有

$set['options'][]['name'] = $o;
                ^
             this one 

$set['options'][]['values']['name'] = $v;
                ^
             This one

在外部以及内部循环中,它将新项添加到数组中(因此存在类似0,1,2 ......的索引),因此无法生成您想要的内容。

您可以像下面一样更正您的数组:

Demo

<?php

$set = array();
$set['name'] = "Product Name";

$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");

echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
    echo "<pre>- $o</pre>";

    $vals = array();
    foreach ($values[$o] as $v) {
        echo "<pre>-- $v</pre>";
         $vals[] = array('name' => $v );
    }
     $set['options'][] = array('name' => $o, 'values' => $vals );

}

print_r($set);

?>