PHP - 检查当前ID是否与foreach循环中的先前ID相同

时间:2016-07-22 14:59:00

标签: php foreach

我有这个private final ConcurrentMap<String, List<String>> entries = new ConcurrentHashMap<String, List<String>>(); public void method3(String key, String value) { List<String> existing = entries.get(key); if (existing != null) { existing.add(value); } else { synchronized (entries) { List<String> existingSynchronized = entries.get(key); if (existingSynchronized != null) { existingSynchronized.add(value); } else { List<String> newList = new ArrayList<>(); newList.add(value); entries.put(key, newList); } } } } 循环:

foreach

修改

我需要根据$ data ['CUST_ID']来区分具有底部边框的用户;

示例:

foreach($dataSet1 as $data) {
    $result .= '<tr>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda"><a href="/lookup.php?userID='.$data['CUST_ID'].'">' . $data['CUST_ID'] . '</a></td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
    $result .= '</tr>';
}

3 个答案:

答案 0 :(得分:6)

只需将以前的ID保存在变量中并进行检查,然后在每次循环迭代中更新它:

$previousId = '';
foreach($dataSet1 as $data):
    if ($previousId !== '' && $previousId !== $data['CUST_ID']) {
        // put a border
    }
    $previousId = $data['CUST_ID'];
    $result .= '<tr>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda"><a href="/lookup.php?userID='.$data['CUST_ID'].'">' . $data['CUST_ID'] . '</a></td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
    $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
    $result .= '</tr>';
endforeach;

答案 1 :(得分:1)

您需要在循环外设置一个保留此信息的变量:

$previous = null;

所以在你的循环中你可以问:

if ($data['CUST_ID'] == $previous) {
    // current customer id same as previous id
} else {
    // not the same
}

在循环的最后,将宝贵的变量设置为当前的变量:

$previous = $data['CUST_ID'];

答案 2 :(得分:1)

/* You have to declare a variable outside of the foreach loop
which will hold your value for previous id as you proceed through the loop.*/

$prev_cust_id = '';    

foreach($dataSet1 as $data):

  // Then inside the loop, declare a the border styling as empty.
  //Make it not empty and as you need it to be if your IDs do not match.

  $add_border = '';
  if($data['CUST_ID'] != $prev_cust_id && $prev_cust_id != ''){    
      $add_border = 'style="border-bottom:1px solid black;"';
  }

  $result .= '<tr>';

  // Assign border styling variable to your HTML element.

  $result .= '<td '.$add_border.' bgcolor="#EBEBEB" width="100" class="veranda"><a href="/lookup.php?userID='.$data['CUST_ID'].'">' . $data['CUST_ID'] . '</a></td>';
  $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['LAST_NAME'] . '</td>';
  $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['PHONE'] . '</td>';
  $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . $data['ORD_COUNT'] . '</td>';
  $result .= '<td bgcolor="#EBEBEB" width="100" class="veranda">' . date('m/d/Y' ,strtotime($data['DATE_LAST'])) . '</td>';
  $result .= '</tr>';

  // Set current ID as previous ID before ending the loop for current ID.
  $prev_cust_id = $data['CUST_ID'];

endforeach;