我想使用以下内容创建和填充数组:
数组代表一天,从7:30到18:00有42个时段。
与此类插槽对应的数组项必须包含客户端名称,插槽开头,插槽结束,客户端可以使用的Vehicle以及if以显示数组记录的标志。
我有14个人,并且应该在顶级数组(14天阵列)中为每个人重复上面的数组。
因此第一级数组表示第1至第14级。第二级数组表示时隙1 - 42.第三级数组表示数据(客户端,车辆等)。
我有以下代码:
$slots = array(
"1" =>
array(
"1" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"2" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"3" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
// ...
"42" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
),
"2" =>
array(
"1" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"2" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"3" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
// ...
"42" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
),
"14" =>
array(
"1" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"2" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
"3" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
// ...
"42" => 0, array("ClientName" => "", "Start" => 0, "End" => 0, "VehicleRegistration" => ""),
),
);
但是人口会产生一个数组,其中我大部分都是0值,而且几乎没有任何数据(Client,...)。似乎大多数信息都丢失了。
我应该如何正确创建这个嵌套数组?
答案 0 :(得分:0)
您的日期数组定义会覆盖自身,因为您混合了特定的数字键/值对和非键控数组:
"1"=>0, array("ClientName"=>"", "Start"=>0, "End"=>0, "VehicleRegistration"=>""),
上述代表两个要素。我分开了这一行来突出这个事实:
"1"=>0,
array("ClientName"=>"", "Start"=>0, "End"=>0, "VehicleRegistration"=>""),
第一个元素放在指定的索引中,即索引为“1”。第二个元素被添加到下一个可用索引,即2,但是在下一行中,您将0重新分配给同一个索引:
"2" => 0,
因此,您为索引2(使用ClientName等)分配的数组将被值0覆盖。
所以它继续;你继续用值0覆盖前面的数组。
你真正想要的是将0值移到其后面的数组中,并命名它(例如“Flag”),如下所示:
array("Flag"=>0, "ClientName"=>"", "Start"=>0, "End"=>0, "VehicleRegistration"=>""),
这是生成一个包含42个插槽一天的数组的代码,乘以14个人,因此您不必逐字输入所有内容:
function createPersonsDay($slots, $persons) {
return array_fill_keys(
range(1, $persons), // number of persons
array_fill_keys(
range(1, $slots), // number of slots
array(
"Flag" => false,
"ClientName" => "",
"Start" => 0,
"End" => 0,
"VehicleRegistration" => ""
)
)
);
}
这样称呼:
$personsDay = createPersonsDay(42, 14);
现在 $ personsDay 是:
array (
1 =>
array (
1 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
2 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
...
42 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
),
2 =>
array (
1 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
...
14 =>
array (
1 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
...
42 =>
array (
'Flag' => false,
'ClientName' => '',
'Start' => 0,
'End' => 0,
'VehicleRegistration' => '',
),
),
)
内部关联数组有一个属性“Flag”,它似乎比一个键(带有数值,标志)和一个非命名数组的混合更合适,因为它出现在你的问题中。