php - Multidimensional array for schedule app -
i create , populate array following:
an array represents day, has 42 time slots, 7:30 18:00.
an array item corresponding such slot, must contain client name, slot start, slot end, vehicle client can use , flag if display array record.
i have 14 persons, , above array should repeated each of them in top-level array (of 14 day-arrays).
so first level array represents persons 1 14. second level array represents time slots 1 - 42. third level array represents data (client, vehicle,... etc).
i have following code:
$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" => ""), ), ); but population results in array have 0 values , hardly data (client, ...). seems information lost.
how should create nested array correctly?
your day-array definition overwrites because have mix of specific numerical key/value pairs , non-keyed arrays:
"1"=>0, array("clientname"=>"", "start"=>0, "end"=>0, "vehicleregistration"=>""), the above line represents 2 elements. split line highlight fact:
"1"=>0, array("clientname"=>"", "start"=>0, "end"=>0, "vehicleregistration"=>""), the first element put in specified index, i.e. @ index "1". second element added next available index, i.e. 2, in next line re-assign 0 same index:
"2" => 0, so array assigned index 2 (with clientname, etc) overwritten value 0.
and continues; keep overwriting previous array value 0.
what want move 0-value inside array follows it, , name (e.g. "flag"), this:
array("flag"=>0, "clientname"=>"", "start"=>0, "end"=>0, "vehicleregistration"=>""), code generate array
here code generate array 42 slots 1 day, multiplied 14 persons, don't have literally type of it:
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" => "" ) ) ); } call this:
$personsday = createpersonsday(42, 14); now $personsday is:
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' => '', ), ), ) the inner associative array has property "flag", seemed more appropriate mix of key (with numerical value, flag), , non-named array, appears in question.
Comments
Post a Comment