用值填充多维数组

时间:2019-06-12 07:05:03

标签: php arrays multidimensional-array

我正在尝试用数据填充多维数组。 有没有一种方法可以细分多维数组,或者我需要运行“ foreach”-循环来填充提到的数组吗?

要插入的数据:

$dice_hand = array(1,2,3,4,5);

应该接收上述输入数据的多维数组框架:

$scorecard = array(
     "player_1" => [
         "scoring" => [
             "rounds" => [
                 "round_1" => [], # Data to be inserted here.
                 "round_2" => [],
                 "round_3" => [],
                 ]
         ]
     ]
);

预期结果:

Array
(
    [player_1] => Array
        (
            [scoring] => Array
                (
                    [rounds] => Array
                        (
                            [round_1] => Array
                                (1,2,3,4,5
                                )

                            [round_2] => Array
                                (
                                )

                            [round_3] => Array
                                (
                                )

                        )

                )

        )

)

Attempt_1 :(将数组数据注入多维数组)。

$scorecard$player_1$scoring$rounds$round_1 = $dice_hand;

1 个答案:

答案 0 :(得分:0)

以防您的问题是PHP语法:

访问数组元素的PHP方法是在密钥周围使用Open and Closed principle[,因此您需要:

]

而不是:

$scorecard["player_1"]["scoring"]["rounds"]["round_1"] = $dice_hand