Hanner
Hanner

Reputation: 371

Formatting php array to a tree like structure

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?

Upvotes: 1

Views: 69

Answers (3)

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

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

Upvotes: 0

Ahmed Ali
Ahmed Ali

Reputation: 1966

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;

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163287

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>

Upvotes: 1

Related Questions