Daniel Iliaguev Harta
Daniel Iliaguev Harta

Reputation: 431

How to print array that sorted by alphabet with the first character as title?

I am creating a plugin for WordPress and I am tiring to sort the names of the users by alphabet and put it into a simple list that have the first character as title.

Here a picture of what I am trying to accomplish : enter image description here

I don't need the "#" part because I have only names and there is no way for them to start like that.

So I have the array $stuck that is already have the values in alphabetic order.

And I am using this :

foreach ($stack as $key => $val) { 
$Firstchar = mb_substr(mb_strtoupper($val, "utf-8"),0,1,'utf-8');  
// getting the first character of each value to create a title

But I really have no idea how to process from here I tried so many different ways and with no success so if anyone can help me figure this out I will be very thank thankful !

I need to do 2 things :

1 .A way to create an alphabetic titles for the names like in the picture, the hard part is that I it to be dependent on the names that exist so if there is no name that start with the letter "b" there is no section of b at all.

  1. A simple example of html of how to do a list like that in the picture.

I am noob but I really need this!

Upvotes: 1

Views: 2193

Answers (3)

gbvisconti
gbvisconti

Reputation: 412

I did something like that a few weeks ago. Maybe It helps.

I create an array where index is the letter and the content is an array with data of person.

$arrayPerson = array(array("name" => "Anderson", "job" => "Some Job"), array("name" => "Barney", "job" => "Some Job"));

$letter = '';

foreach ($arrayPerson as $personData) {

    if (!$letter || strtolower($personData['name'][0]) != $letter) {

        $letter = strtolower($personData['name'][0]);
    }

    $html[$letter][] = array($personData["name"], $personData["job"]);
}

Then I just print the content of $html

foreach($html as $letter => $innerArray) {

    print $letter."<br />";

    foreach($innerArray as $data) {

        print $data[0]."-".$data[1];

    }
}

Upvotes: 4

skobaljic
skobaljic

Reputation: 9634

Even if words/titles are not sorted (as in following example), you can do it this way:

<?php

$stack = array(
    'Acid Factory',
    'Acno\'s Energizer',
    'Alex in Danger',
    'Cable Capers 3',
    'Hangaroo',
    'Evac',
    'Exile',
    'Doodle',
    'FA-18',
    'Gun Run',
    'Egg Run',
    'Heli Attack'
);

function make_dictionary( &$words ) {
    foreach ( $words as $word ) 
        if (!in_array( strtoupper( $word[0] ), $words )) $words[] = strtoupper( $word[0] );
    natcasesort( $words );
};

function print_dictionary( $words ) {
    echo '<ul>';
    foreach ( $words as $word ) 
        ( strlen( $word )==1 ) ? print_title( $word ) : print_word( $word );
    echo '</ul>';
};

function print_title( $word ) {
    echo '<li><h3>' . $word . '</h3></li>';
};

function print_word( $word ) {
    echo '<li>' . $word . '</li>';
};

make_dictionary( $stack );
print_dictionary( $stack );

/* This will output HTML: 
<ul>
    <li>
        <h3>A</h3>
    </li>
    <li>Acid Factory</li>
    <li>Acno's Energizer</li>
    <li>Alex in Danger</li>
    <li>
        <h3>C</h3>
    </li>
    <li>Cable Capers 3</li>
    <li>
        <h3>D</h3>
    </li>
    <li>Doodle</li>
    <li>
        <h3>E</h3>
    </li>
    <li>Egg Run</li>
    <li>Evac</li>
    <li>Exile</li>
    <li>
        <h3>F</h3>
    </li>
    <li>FA-18</li>
    <li>
        <h3>G</h3>
    </li>
    <li>Gun Run</li>
    <li>
        <h3>H</h3>
    </li>
    <li>Hangaroo</li>
    <li>Heli Attack</li>
</ul>
*/

?>

Upvotes: 1

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

Key-value approach - live example

If $stack is arranged like this;

$array = array("a" => array("apples", "ankle"),
               "b" => array("batman", "bruce"),
               "c" => array("comic", "cat")
              );

Then we can do a nested foreach

$array = array("a" => array("apples", "ankle"),
               "b" => array("batman", "bruce"),
               "c" => array("comic", "cat")
              );

foreach($array as $key => $values) {
   echo $key . PHP_EOL . PHP_EOL;
   echo "<ul>";
   foreach($values as $vals) {
       echo "<li>". $vals ."</li>";
   }
   echo "</ul> <br />";
}

Just-value approach - live example

Otherwise, keep track of the letter.

$array = array("apples", "ankle", "batman", "bruce", "comic", "cat");

$current_letter = strtoupper( substr($array[0], 0, 1) );  //Grab the starting letter         

echo $current_letter . PHP_EOL;

foreach($array as $values) { //Loop through our array
    if( strtoupper($values[0]) != $current_letter ) { //If the first letter doesn't match the title
        $current_letter = strtoupper($values[0]); //Overwrite the title letter
        echo PHP_EOL . strtoupper($current_letter) . PHP_EOL; //Echo the title letter
    }  

    echo $values . "<br />"; //Echo the word
}

Upvotes: 3

Related Questions