在关联数组中获取先前的数组键

时间:2013-08-19 03:13:29

标签: php arrays foreach

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

<?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>

如何只显示名字中每个字母的标签一次? 以下是我想要完成的结果。

enter image description here

感谢。

4 个答案:

答案 0 :(得分: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.
}

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

答案 1 :(得分: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变量存储上一个项目,该项目会根据 $previous更新前的当前项目进行检查。请记住,PHP逐行执行。如果上一个结果的第一个字母不等于上一个项目的第一个字母,那么让我们添加它。否则,添加一个不间断的空白字符以保留表格单元格的可见性。

这似乎正是你想要做的。有一些方法可以清理它,但它完全符合您的要求。 See the code

答案 2 :(得分: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>";
}

答案 3 :(得分: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>