如何在使用数组项目之前检查?

时间:2016-05-30 17:58:14

标签: php html arrays

我有一个这样的数组:

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
Array
(
    [0] => Array
        (
            [id] => 191
            [name] => Ali
            [range] => today
        )

    [1] => Array
        (
            [id] => 190
            [name] => John
            [range] => today
        )

    [2] => Array
        (
            [id] => 189
            [name] => peter
            [range] => in last week
        )

    [3] => Array
        (
            [id] => 180
            [name] => Ali
            [range] => in last week
        )

    [4] => Array
        (
            [id] => 170
            [name] => Jack
            [range] => in last week
        )
)
*/

我试图像这样动态制作HTML:

<div id = "notification_box">
    <div class= "cases">
        <div class="title">today</div>                        
        <table>
            <tr>
                <td>191</td>
                <td>Ali</td>
            </tr>
            <tr>
                <td>190</td>
                <td>John</td>
            </tr>
        </table>
    </div>
    <div class= "cases">
        <div class="title">yesterday</div>                   
    </div>
    <div class= "cases">
        <div class="title">in last week</div>    
        <table>
            <tr>
                <td>189</td>
                <td>Peter</td>
            </tr>
            <tr>
                <td>180</td>
                <td>Ali</td>
            </tr>
            <tr>
                <td>170</td>
                <td>Jack</td>
            </tr>
        </table>
    </div>
</div>

这是我的代码:

$html = '';
$last_item_range = '';

foreach ( $results as $key => $item )
{
  $next_item = ( is_array($results[$key+1]) ) ? $results[$key+1] : null;

    if ( $item['range'] != $last_item_range )
    {
        $html .= '<div class="cases">';
        $html .=   sprintf('<div class="title">%s</div>', $item['range']);
        $html .=   '<table>';

        $last_item_range = $item['range'];
    }

    $html .= '<tr>';
    foreach($item as $value){ $html .= sprintf('<td>%s</td>', $value); }
    $html .= '</tr>';

    if ( $next_item['range'] != $item['range'] )
    {
      $html .=  '</table>';
      $html .= '</div>';
    }
}

echo $html;

但是这一行$results[$key+1]引发了这个通知:

  

注意:未定义的偏移量

我该如何解决?

也请你告诉我,我做得对吗?或者有一个更好的算法来制作HTML?

1 个答案:

答案 0 :(得分:1)

使用两个循环尝试此解决方案

$new_list =  array();
foreach( $array_list as  $list )
$new_list[$list['range']][] = $list;

?>
<div id="notification_box">
   <?php foreach( $new_list as $item_name => $items ): ?> 
    <div class= "cases">
        <div class="title"><?php echo $item_name ?></div>
        <?php 
          if( $items && is_array( $items )  ) {

             ?>
              <table>
                 <?php foreach( $items  as $item ): ?>
                 <tr>
                   <td><?php echo $item['id'] ?></td>
                   <td><?php echo $item['name'] ?></td>
                 </tr>
                 <?php endforeach; ?> 
              </table>
             <?php 

          }    
         ?>
    </div>
   <?php endforeach; ?> 
</div>

查看完整结果:https://3v4l.org/CHpm9