如何使用PHP在Foreach中生成表列?

I'm fetching data from a website with Json Objects and I will show it in table as the last images (check it bottom) but I'm really complicated with luckNumber and productName because I will create 5 column for this table and insert value to each Draw types.

Here is Json data

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW3' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW3' (length=5)

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW4' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW4' (length=5)

object(stdClass)[5]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[4]
      public 'luckDrawDesc' => string 'DRAW3' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW3' (length=5)

object(stdClass)[3]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[2]
      public 'luckDrawDesc' => string 'DRAW2' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW2' (length=5)

object(stdClass)[1]
  public 'luckNumber' => string '78087' (length=5)
  public 'productJson' =>
    object(stdClass)[0]
      public 'luckDrawDesc' => string 'DRAW1' (length=5)
      public 'validPeriod' => int 2
  public 'productName' => string 'DRAW1' (length=5)

This is PHP function to select all that data and generate tables

Function description:

luckNumber Contain a defference value depend on productName (DRAW1,DRAW2,DRAW3,DRAW14).

public function det_data() {

    $c_arr = json_decode($this->urlData);
    $i = 0;
    $html = '<table>';
    $html .= '<thead>';
    $html .= '<th>Date</th>';
    $html .= '<th>Draw1</th>';
    $html .= '<th>Draw2</th>';
    $html .= '<th>Draw3</th>';
    $html .= '<th>Draw4</th>';
    $html .= '</thead>';
    $html .='<tbody>';
    foreach ($c_arr as $items) {
        if ($i == 30)
            break;
        {

            $html .= '<tr>';
            if ($items->productName =='DRAW1') {
                $html .= '<td>' . $items->createTime . '</td>';
                $html .= '<td>' . $items->luckNumber . '</td>';  
            } 
            if ($items->productName =='DRAW2') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            if ($items->productName =='DRAW3') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            if ($items->productName =='DRAW4') {
                $html .= '<td>' . $items->luckNumber. '</td>';  
            } 
            $html .= '</tr>';

            $i++;
        }
    }
    $html .='</tbody>';
    $html .= '</table>';
    return $html;
}

The incorrect result enter image description here

The result that I want to get 78087 This number for example only enter image description here

I would try first to collect all data in structure like this one:

array (unix_timestamp_of_date => array('DRAW1' => xxxxx, 'DRAW2' => xxxxx, 'DRAW3' => xxxxx, 'DRAW4' => xxxxx))

This way you're not dependent of whenever each DRAW data you get is sorted by time or not.

Then you can easily output this table.

Try something like this:

// 1. collect data
$output = array();
foreach ($c_arr as $items) {
    $timestamp = strtotime($items->createTime);
    if (!isset($output[$timestamp])) $output[$timestamp] = array();

    $output[$timestamp][$items->productName] = $items->luckNumber;
}

// uncomment below line if you want to sort your results by time
//ksort($output);


// 2. print data
foreach ($output as $timestamp => $data) {
    $html .= '<tr>';
    $html .= '<td>'.date('Y-m-d H:i:s', $timestamp).'</td>';
    for ($draw = 1; $draw <= 4; $draw++) {
        $html .= '<td>';
        $html .= isset($data['DRAW'.$draw]) ? $data['DRAW'.$draw] : 0; // will display 0 if there is no lucky number
        $html .= '</td>';
    }
    $html .= '</tr>';
}