PHP multidimensional array and while i++
Get the solution ↓↓↓So I'm having a problem creating the table with "names". I only got two results instead of 4. Here is my multidimensional array:
Array
(
[0] => Array
(
[start] => 1
[name] => Test_name_1
)
[1] => Array
(
[start] => 6
[name] => Test_name_2
)
[2] => Array
(
[start] => 22
[name] => Test_name_3
)
[3] => Array
(
[start] => 23
[name] => Test_name_4
)
)
Then I want to create table with$i=1;
and until 47. So my code here is:
$i=1;
while ($i<=47){
foreach($array as $test){
echo '<tr>';
if ($test['start']==$i){
echo '<td align="center" style="border-top: 1px solid black;"><b>'.$test['name'].'</b> </td>';
}else{
echo '<td align="center" style="border-top: 1px solid black;">blank </td>';
}
echo '<td align="center" style="border-top: 1px solid black;">'.$i.' </td>';
$i++;
echo '<tr>';
};
}
The result I receive only displays 2 names instead of 4 in the array:
Why are Test_name_3 and Test_name_4 excluded from my result?
Answer
Solution:
Following logic might help you solve your issue:
<?php
$arr = [
['start' => 1, 'name' => 'Test_name_1'], ['start' => 6, 'name' => 'Test_name_2'],
['start' => 22, 'name' => 'Test_name_3'], ['start' => 23, 'name' => 'Test_name_4'],
];
$hit = false;
echo '<table>';
for($i = 1; $i <= 47; $i++) {
echo '<tr>';
foreach($arr as $record) {
if($record['start'] == $i) {
$hit = true;
echo '<td align="center" style="border-top: 1px solid black;"><b>' . $record['name'] . '</b> </td>';
echo '<td align="center" style="border-top: 1px solid black;">' . $i . ' </td>';
break;
}
}
if(!$hit) {
echo '<td align="center" style="border-top: 1px solid black;">blank </td>';
echo '<td align="center" style="border-top: 1px solid black;">' . $i . ' </td>';
}
echo '</tr>';
$hit = false;
}
echo '</table>';
Answer
Solution:
$i = 1;
while ($i <= 47) {
foreach ($array as $test) {
echo '<tr>';
if ($test['start'] == $i) {
echo '<td align="center" style="border-top: 1px solid black;"><b>' . $test['name'] . '</b> </td>';
} else {
echo '<td align="center" style="border-top: 1px solid black;">blank </td>';
}
echo '<td align="center" style="border-top: 1px solid black;">' . $i . ' </td>';
echo '<tr>';
};
$i++;
}
Take $i++ outside of foreach loop!!!
Answer
Solution:
this might solve your problem with while loop
$array = [
['start' => 1, 'name' => 'Test_name_1'], ['start' => 6, 'name' => 'Test_name_2'],
['start' => 22, 'name' => 'Test_name_3'], ['start' => 23, 'name' => 'Test_name_4'],
];
echo '<table>';
$i=1;
while ($i<=47){
$count=0;
foreach($array as $test){
if (((int)$test['start'])==$i){
echo '<tr>';
echo '<td align="center" style="border-top: 1px solid black;">
<b>'.$test['name'].'</b> </td>';
echo '<td align="center" style="border-top: 1px solid black;">'.$i.' </td>';
echo '</tr>';
}else{
$count=$count+1;
if( $count==count($array))
{
echo '<tr>';
echo '<td align="center" style="border-top: 1px solid black;">blank </td>';
echo '<td align="center" style="border-top: 1px solid black;">'.$i.' </td>';
echo '</tr>'; }
}
}
$count=0;
$i++;
}
echo '</table>';
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.