计划应用的多维数组



我想用以下内容创建和填充一个数组:

数组表示一天,有 42 个时隙,从 7:30 到 18:00。

与此类插槽对应的数组项必须包含客户端名称、插槽开始、插槽结束、客户端可以使用的车辆以及用于显示阵列记录的标志。

我有 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 个值,几乎没有任何数据(客户端,...似乎大多数信息都丢失了。

我应该如何正确创建此嵌套数组?

您的日数组定义会覆盖自身,因为您混合了特定的数字键/值对和非键数组:

"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",这似乎比键(带有数值,标志(和非命名数组的组合更合适,因为它出现在您的问题中。

最新更新