1

Hi I have this code and array:

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

How can I display once only the label per letter of the first name? Below is the result I want to accomplish.

enter image description here

Thanks.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
marknt15
  • 5,047
  • 14
  • 59
  • 67

4 Answers4

2

What you want to do, is to store the value of an entry after handling said entry in a seperate variable, so you can use it on your next iteration.

For example:

$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.
}

How to apply this to your situation, is left as an exercise for the reader.

alexanderpas
  • 2,712
  • 2
  • 23
  • 33
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>

The general idea is that you want to store the previous result in a variable before you compare what the current items and what the previous item is. The $previous variable store the previous item which is checked against the current item before $previous is updated. Remember that PHP executes line by line. If the first letter of the previous result does not equal the first letter of the previous item, then let's add it. Otherwise, add a non-breaking whitespace character to preserve the table cell's visibility.

This seems to be exactly what you want to do. There are ways you can clean this up but it's exactly as you want it. See the code

djthoms
  • 3,026
  • 2
  • 31
  • 56
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>";
}
Soosh
  • 812
  • 1
  • 8
  • 24
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>
MaveRick
  • 1,181
  • 6
  • 20