Home » Php » How to display an Array under alphabetical letters using PHP?

How to display an Array under alphabetical letters using PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have an array that already contains all it’s values in alphabetical order:

Alligator
Alpha
Bear
Bees
Banana
Cat
Cougar

Now I just want to list the first letter each starts with above it like so:

A
---

Alligator
Alpha

B
---

Bear
Bees
Banana

C
---

Cat
Cougar

etc...

How can this be done?

How to&Answers:

The solution is to keep in a variable the first letter of the previously printed word, like:

$previous = null;
foreach($array as $value) {
    $firstLetter = substr($value, 0, 1);
    if($previous !== $firstLetter) echo "\n".$firstLetter."\n---\n\n";
    $previous = $firstLetter;

    echo $value."\n";
}

NB: if some entries start with a lower-case letter and others with upper-case letters, use the strcasecmp function in the test instead of !==.

Answer:

here is another simple solution:-

$result = array();
foreach ($list as $item) {
    $firstLetter = substr($item, 0, 1);
    $result[$firstLetter][] = $item;
}

echo "<pre>"; print_r($result);

Output :-

Array (
[A] => Array
    (
        [0] => Alligator
        [1] => Alpha
    )

[B] => Array
    (
        [0] => Bear
        [1] => Bees
        [2] => Banana
    )

[C] => Array
    (
        [0] => Cat
        [1] => Cougar
    )
 )

Answer:

Well i have three solutions.
1) Make another array containing all alphabets. Then use foreach to iterate through this array. And in nested foreach check for occurance of that letter through strpos method of php.
Here is rough code.

<?php
$alphabets = array ("A","B","C"....); //for all alphabtes
foreach($alphabets as $alphabet)
{
    echo $alphabet;
    foreach($myArray as $arr)
    {
         $pos = strpos($arr, $alphabet);
         if($pos===flase)
         {
             //do nothing
         }
         else
         {
             echo $arr;
         }
}
?>

2) second method have same logic as above. But here you dont need to make array for alphabets. You can get all alphabets this way.

<?php
foreach(range('a', 'z') as $letter) {
    echo $letter;
}
?>

Php range method

3) Third solution also have same logic as above two. Here you can get alphabets by another way 🙂

for ($i=65; $i< =90; $i++) {
 $x = chr($i);
 print $x;
}

Answer:

Iterate through the array and check if the current item starts with another letter than the previous one. If that’s the case print your “A —“.

$currentLetter = '';
foreach ($list as $item) {
   $firstLetter = substr($item, 0, 1);
   if ($firstLetter !== $currentLetter) {
      echo $firstLetter . "\n---\n";
      $currentLetter = $firstLetter;
   }
   echo $item . "\n";
}

Answer:

For HTML output:

<h3>A</h3>
<ol>
   <li>
      <em>tag count</em>
      <a href="link to tag">Animal</a>
   </li>
   <li>
      <em>tag count</em>
      <a href="link to tag">Aqua</a>
   </li>
   <li>
      <em>tag count</em>
      <a href="link to tag">Arthur</a>
   </li>
</ol>
<!-- if B not EXIST not show B -->
<h3>C</h3>
<ol>
   <li>
      <em>tag count</em>
      <a href="link to tag">Camel</a>
   </li>
   <li>
      <em>tag count</em>
      <a href="link to tag">Crazy</a>
   </li>
</ol>
<!-- etc -->

I change Artefact2 code and share for you

<?php $previous = null;
    foreach ($dataProvider as $key => $tag) { 
        $firstLetter = strtoupper(substr($tag['name'], 0, 1));
        if($previous !== $firstLetter) { 
            if($key != 0) {
                echo '</ol>';
            }?>
            <h3 class="alpha">
                <span><?php echo $firstLetter;?></span>
            </h3>
            <ol class="tags main">
        <?php } ?>
                <li class="tag">
                    <em><?php echo $tag['TagsCount']; ?></em>
                    <a href="<?php echo $tag['slug']; ?>">
                        <strong><?php echo ucfirst($tag['name']); ?></strong>
                    </a>
                    <span class="perc" style="width: 90px;"></span>
                </li>
        <?php if($key == count($dataProvider)-1) { 
            echo '</ol>';
        }
            $previous = $firstLetter; 
    }
?>

Best Regards www.Forum.iSYSTEMS.am