1

嗨,我有这个代码和数组:

<?php

$arr = array(
    0 => array('first_name' => 'Ace', 'last_name' => 'Jones'), 
    1 => array('first_name' => 'Aron', 'last_name' => 'Jones'), 
    2 => array('first_name' => 'Ben', 'last_name' => 'Jones'), 
    3 => array('first_name' => 'Billy', 'last_name' => 'Jones'), 
    4 => array('first_name' => 'Barney', 'last_name' => 'Jones'), 
    5 => array('first_name' => 'Con', 'last_name' => 'Jones'), 
    6 => array('first_name' => 'Dan', 'last_name' => 'Jones'), 
    7 => array('first_name' => 'Earl', 'last_name' => 'Jones'), 
    8 => array('first_name' => 'East', 'last_name' => 'Jones'), 
    9 => array('first_name' => 'Fez', 'last_name' => 'Jones')
);

$html = '';
foreach($arr as $k => $v) {
    echo $v['first_name'] . '<br />';
}
?>

<table rules="all" style="border:1px solid blue;" cellspacing="2" cellpadding="2">
    <tr>
        <td>Label</td>
        <td>First Name</td>
        <td>Last Name</td>
    </tr>
    <?php echo $html; ?>
</table>

我怎样才能只显示名字的每个字母的标签?下面是我想要完成的结果。

在此处输入图像描述

谢谢。

4

4 回答 4

2

您想要做的是在处理所述条目后将条目的值存储在单独的变量中,以便您可以在下一次迭代中使用它。

例如:

$previous = '';
foreach($array as $key => $value) {
  if($previous != $value) {
    /* insert code that only runs if previous is not equal here. */
  }

  /* insert code that is ran every time here. */

  $previous = $value; //stores the value so it can be used on the next iteration.
}

如何将此应用于您的情况,留给读者作为练习。

于 2013-08-19T03:32:16.813 回答
2
<?php
$arr = array(
    0 => array('first_name' => 'Ace', 'last_name' => 'Jones'), 
    1 => array('first_name' => 'Aron', 'last_name' => 'Jones'), 
    2 => array('first_name' => 'Ben', 'last_name' => 'Jones'), 
    3 => array('first_name' => 'Billy', 'last_name' => 'Jones'), 
    4 => array('first_name' => 'Barney', 'last_name' => 'Jones'), 
    5 => array('first_name' => 'Con', 'last_name' => 'Jones'), 
    6 => array('first_name' => 'Dan', 'last_name' => 'Jones'), 
    7 => array('first_name' => 'Earl', 'last_name' => 'Jones'), 
    8 => array('first_name' => 'East', 'last_name' => 'Jones'), 
    9 => array('first_name' => 'Fez', 'last_name' => 'Jones')
);
sort($arr); // ensure correct order
$html = '';
foreach($arr as $k => $v) {
    if(substr($v['first_name'], 0, 1) != $previous) {
        $html .= '<tr><td>' . substr($v['first_name'], 0, 1) . '</td>';
    } else {
        $html .= '<tr><td>&nbsp;</td>';
    }
    $html .= '<td>' . $v['first_name'] . '</td>';
    $html .= '<td>' . $v['last_name'] . '</td></tr>';
    $previous = substr($v['first_name'], 0, 1);
}
?>

<table rules="all" style="border: 1px solid blue;" cellspacing="2" cellpadding="2">
    <tr>
        <td>Label</td>
        <td>First Name</td>
        <td>Last Name</td>
    </tr>
    <?php echo $html; ?>
</table>

一般的想法是,在比较当前项目和前一个项目是什么之前,您希望将先前的结果存储在一个变量中。变量存储前一项,在更新之前$previous对照当前项进行检查。请记住,PHP 是逐行执行的。如果上一个结果的第一个字母不等于上一项的第一个字母,那么让我们添加它。否则,添加一个不间断的空白字符以保持表格单元格的可见性。 $previous

这似乎正是您想要做的。有一些方法可以清理它,但它正是你想要的。查看代码

于 2013-08-19T03:34:13.087 回答
0
$html = "";
foreach($arr as $a) 
{
    $html .= "<tr>";
    $temp = 1;
    foreach($a as $k => $v)
    {
         if($temp == 1)
         {
             $html .= "<td>" . ucfirst($v['first_name'][0]) . "</td>";
         } 

         $html .= "<td>" . $v['first_name'] . "</td><td>" . $v['last_name'] . "</td>";
         $temp++; 
    }
    $html .= "</tr>";
}
于 2013-08-19T03:21:05.563 回答
0
$arr = array(
    0 => array('first_name' => 'Ace', 'last_name' => 'Jones'), 
    1 => array('first_name' => 'Aron', 'last_name' => 'Jones'), 
    2 => array('first_name' => 'Ben', 'last_name' => 'Jones'), 
    3 => array('first_name' => 'Billy', 'last_name' => 'Jones'), 
    4 => array('first_name' => 'Barney', 'last_name' => 'Jones'), 
    5 => array('first_name' => 'Con', 'last_name' => 'Jones'), 
    6 => array('first_name' => 'Dan', 'last_name' => 'Jones'), 
    7 => array('first_name' => 'Earl', 'last_name' => 'Jones'), 
    8 => array('first_name' => 'East', 'last_name' => 'Jones'), 
    9 => array('first_name' => 'Fez', 'last_name' => 'Jones')
);

$html = '';
foreach($arr as $k => $v) {
    $lable = substr($v['first_name'], 0, 1);
    $html[$lable][] = array($v['first_name'],$v['last_name']);
}
?>
<table rules="all" style="border:1px solid blue;" cellspacing="2" cellpadding="2">
    <tr>
        <td>Label</td>
        <td>First Name</td>
        <td>Last Name</td>
    </tr>
    <?php 
    ksort($html);
    foreach ($html as $k => $v) {
        foreach ($v as $ke => $va) {
            echo "<tr>";
            echo "<td>".($ke==0?$k:null)."</td>";
            echo "<td>".$va[0]."</td>";
            echo "<td>".$va[1]."</td>";
            echo "</tr>";
        }
    }
     ?>
</table>
于 2013-08-19T03:37:12.567 回答