Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function callsCached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable namesUsed descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

added 29 characters in body
Source Link
jsanc623
  • 2.8k
  • 15
  • 22

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l < count($day); ++$l) {
    for ($k = 0; $k < count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};

Which would generate:

Array
(
    [MonP1B] => 
    [MonP2B] => 
    [MonP3B] => 
    ...
    [FriP4B] => 
    [FriP5B] => 
    [FriP6B] => 
)

And which you can access via $arr['FriP5B'] for example.

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day < $number_of_days; ++$day) {
    for ($room = 0; $room < $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);

What we did:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. Stored everything in 1 array, so we don't have to chase 100 different variables

Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.

Source Link
jsanc623
  • 2.8k
  • 15
  • 22
Loading