Create a string one character at a time using a loop

I am trying to create a line every hour of the week starting at 0. The string is 167 characters long and will consist of 0/1 for each character representing true / false for this hour.

I know that you can edit lines like this:

$foo = "123456789";
echo $foo[0]; // outs 1
echo $foo[1]; //outs 2

$foo[0] = 1;
$foo[1] = 1;

echo $foo[0]; // outs 1
echo $foo[1]; // outs 1

So, I assumed that I could set an empty string and use the same method to build it. Thus, I start the cycle from 0-167 and at the same time check if the "hour" is in the array $hours. If it is, I set it to 1 on my line ... if not it is set to 0. Problems:

1. This does not work by setting $inputHours = '', and it just creates an array instead of the string I want.

2. ,    in_array 167 ?

//array of selected hours
$hours = array(1, 25, 34, 76, 43)

//create selected hours string from selection
$inputHours = '';

for ($x = 0; $x < 168; $x++) {
    $inputHours[$x] = in_array($x, $hours) ? 1 : 0; 
} 

$inputHours = '0011010101...' , 167.

+4
1

.= ([expression] ? [true] : [false]) in_array():

for($x = 0; $x < 168; $x++)
    $inputHours .= in_array($x, $hours) ? 1 : 0;

+11

Source: https://habr.com/ru/post/1541050/


All Articles