1

I have the following array:

$array = [
    1 => [
        'categoryName'=>'Main One',
        'title'=>'Title one',
        'date'=>'2019-09-01',
        'score'=>2500
    ],
    2 => [
        'categoryName'=>'Main One',
        'title'=>'Title two',
        'date'=>'2019-09-02',
        'score'=>3000
    ],
    3 => [
        'categoryName'=>'Main Two',
        'title'=>'Other title',
        'date'=>'2019-09-02',
        'score'=>2100
    ],
    4 => [
        'categoryName'=>'Main Three',
        'title'=>'Other title',
        'date'=>'2019-09-05',
        'score'=>200
    ],
    5 => [
        'categoryName'=>'Main Three',
        'title'=>'Other topic',
        'date'=>'2019-09-02',
        'score'=>1000
    ]
];

So if I run a foreach loop on it:

$html = '';
$html = '<table>';
foreach($array as $row){
    $html .= '<tr>';
    $html .= '<td>'.$row['categoryName'].'</td>';
    $html .= '<td>'.$row['title'].'</td>';
    $html .= '<td>'.$row['date'].'</td>';
    $html .= '<td>'.$row['score'].'</td>';
    $html .= '<tr>';
}
$html .='</table>';
echo $html;

This gives me the following output:

<table border=1>
<tbody>
<tr>
<td>Main One</td>
<td>Title one</td>
<td>2019-09-01</td>
<td>2500</td>
</tr>
<tr>
<td>Main One</td>
<td>Title two</td>
<td>2019-09-02</td>
<td>3000</td>
</tr>
<tr>
<td>Main Two</td>
<td>Other title</td>
<td>2019-09-02</td>
<td>2100</td>
</tr>
<tr>
<td>Main Three</td>
<td>Other title</td>
<td>2019-09-05</td>
<td>200</td>
</tr>
<tr>
<td>Main Three</td>
<td>Other topic</td>
<td>2019-09-02</td>
<td>1000</td>
</tr>
</tbody>
</table>

But I want the output in this format.

<table border=1>
  <tbody>
    <tr>
      <td colspan="2">Main One</td>
    </tr>
    <tr>
      <td>2019-09-01</td>
      <td>Title One</td>
      <td>2500</td>
    </tr>
    <tr>
      <td>2019-09-02</td>
      <td>Title Two</td>
      <td>3000</td>
    </tr>
    <tr>
      <td colspan="2">Main Two</td>
    </tr>
    <tr>
      <td>2019-09-02</td>
      <td>Other title</td>
      <td>2100</td>
    </tr>
    <tr>
      <td colspan="2">Main Three</td>
    </tr>
    <tr>
      <td>2019-09-05</td>
      <td>Other Title</td>
      <td>200</td>
    </tr>
    <tr>
      <td>2019-09-02</td>
      <td>Other topic</td>
      <td>1000</td>
    </tr>
  </tbody>
</table>

I could not get my head around on how to do it inside the foreach loop. What I did try:

 $prevmain = "";
 foreach($array as $row):
    $currentMain = $row['categoryName'];
    if($currentMain!=$prevMain){
       $html .='<tr>
                <td colspan="2">'.$row['currentMain'].'</td>
                <td></td>
               </tr>';
    } else{
       //Here I miss data for the first loop
       $html .='<tr>
                <td>'.$row['date].'</td>
                <td>'.$row['title'].'</td>
                <td>'.$row['score].'</td>
               </tr>';
     }
    $prevmain = $row['categoryName'];        
 endforeach;

As you can see from my attempt code, I miss one row for each loop of the main category. Can anyone help me?

4
  • The fact that the “main” value changed between the previous and the current item, does not mean that the data for the current item does not need to get printed at all - but you made printing it the else branch in your code. Commented Nov 1, 2019 at 12:22
  • (Btw., that will of course only work if those main values can occur two times at most. If that’s not the case, and it could be more, you’d need a more dynamic solution to figure out the correct colspan value to begin with.) Commented Nov 1, 2019 at 12:23
  • @04FS, thanks for the comment. It's just my attempted code. Please have a look at the expected output table in the question. Commented Nov 1, 2019 at 12:37
  • 1
    I don’t need to have a second look, I understood what you want already. That’s why I tried to give you an explanation of what you are currently doing wrong … Did you have trouble understanding what I was trying to say, or …? Commented Nov 1, 2019 at 12:51

3 Answers 3

1

I think you don't need the else and only add the colspan part when the $prevMain and $currentMain are different.

$prevMain = "";
$currentMain = "";
$html = "";
foreach($array as $row):
    $currentMain = $row["categoryName"];
    if($currentMain !== $prevMain) {
        $prevMain = $row["categoryName"];
        $html .= '<tr>' .
            '<td colspan="2">' . $row['categoryName'] . '</td>' .
            '<td></td>' .
            '</tr>';
    }
    $html .='<tr>'.
            '<td>'.$row['date'] .'</td>'.
            '<td>'.$row['title'].'</td>'.
            '<td>'.$row['score'] . '</td>'.
           '</tr>';
endforeach;

echo $html;

See a php demo

Note that I have renamed $prevmain to $prevMain

After formatting the table row structure output will look like:

<tr>
    <td colspan="2">Main One</td>
    <td></td>
</tr>
<tr>
    <td>2019-09-01</td>
    <td>Title one</td>
    <td>2500</td>
</tr>
<tr>
    <td>2019-09-02</td>
    <td>Title two</td>
    <td>3000</td>
</tr>
<tr>
    <td colspan="2">Main Two</td>
    <td></td>
</tr>
<tr>
    <td>2019-09-02</td>
    <td>Other title</td>
    <td>2100</td>
</tr>
<tr>
    <td colspan="2">Main Three</td>
    <td></td>
</tr>
<tr>
    <td>2019-09-05</td>
    <td>Other title</td>
    <td>200</td>
</tr>
<tr>
    <td>2019-09-02</td>
    <td>Other topic</td>
    <td>1000</td>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

0

Update your foreach with this:

$html = '';
$html = '<table>';
foreach($array as $row){
    $html .= '<tr>';
    $html .= '<td colspan="2">'.$row["currentMain"].'</td>';
    $html .= '<td></td>';
    $html .= '</tr>'
    $html .= '<tr>';
    $html .= '<td>'.$row['categoryName'].'</td>';
    $html .= '<td>'.$row['title'].'</td>';
    $html .= '<td>'.$row['date'].'</td>';
    $html .= '<td>'.$row['score'].'</td>';
    $html .= '</tr>';
}
$html .='</table>';
echo $html;

3 Comments

Please look at the expected output table in the question.
<table><tr><td colspan="2">Main One</td></tr><tr><td>Main One</td><td>Title one</td><td>2019-09-01</td><td>2500</td><tr><tr><td colspan="2">Main One</td></tr><tr><td>Main One</td><td>Title two</td><td>2019-09-02</td><td>3000</td><tr><tr><td colspan="2">Main Two</td></tr><tr><td>Main Two</td><td>Other title</td><td>2019-09-02</td><td>2100</td><tr><tr><td colspan="2">Main Three</td></tr><tr><td>Main Three</td><td>Other title</td><td>2019-09-05</td><td>200</td><tr><tr><td colspan="2">Main Three</td></tr><tr><td>Main Three</td><td>Other topic</td><td>2019-09-02</td><td>1000</td><tr></table>
Also there is some syntax error. If you compare with my expected output. You may see the difference.
0

This is a multidimensional array. You can use key in your foreach loop:

foreach($array as $key => $row) {
   echo $row['categoryName']; ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.