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.

2
  • Hi alexanderpas, thanks. I will check it now :) Commented Aug 19, 2013 at 3:23
  • Just remember the first letter of each first_name field; if it's different, show the letter and update the value. Commented Aug 19, 2013 at 3:31

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Thanks djthoms :) This is exactly what I need. I will just simplify the code.
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>";
}

Comments

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.