1

I have a php code as shown below.

php code:

$beta_lists = array_flip($flip);
print_r($beta_lists); // Line A

foreach ($beta_lists as $title => $permalink) {
    $title_char = substr(transliterator_transliterate('Any-Latin;Latin-ASCII;', $title), 0, 1);
}

Line A prints the following arrary:

Array
(
    [Apple] => http://www.abc.mno/apple/
    [Ball] => http://www.abc.mno/ball/
    [Builders] => http://www.abc.mno/builders/
    [Bowling] => http://www.abc.mno/bowling/
    [Correct] => http://www.abc.mno/correct/
    [Campaign] => http://www.abc.mno/compain/
    [Direct] => http://www.abc.mno/direct/
    [Degree] => http://www.abc.mno/degree/
)

What I am trying to achieve through the php code above is I want to count (which is 4 in the following case) the title character while grouping array results in alphabetical order. I will be grouping array results in alphabetical order later.

A                  C

Apple            Correct

B                Compaingn

Ball                D  

Builders         Direct 

Bowling          Degree 

Problem Statement:

I am wondering what changes I should make in the php code above so that it counts the total number of title characters (which is 4) used while grouping array results in alphabetical order.

user1950349
  • 4,738
  • 19
  • 67
  • 119

1 Answers1

1

One option would be to sort the titles into a new array, grouping on the first character of the title. If you ksort the array first, that sorting will be maintained in the new array. Then to get the number of title characters you can simply count the result array. This structure also has the benefit of making it easy to count titles beginning with e.g. 'C' using count($groups['C']).

ksort($beta_lists);
$groups = array();
foreach ($beta_lists as $title => $value) {
    $groups[$title[0]][$title] = $value;
}
print_r($groups);
echo count($groups);

Output:

Array
(
    [A] => Array
        (
            [Apple] => http://www.abc.mno/apple/
        )
    [B] => Array
        (
            [Ball] => http://www.abc.mno/ball/
            [Bowling] => http://www.abc.mno/bowling/
            [Builders] => http://www.abc.mno/builders/
        )
    [C] => Array
        (
            [Campaign] => http://www.abc.mno/compain/
            [Correct] => http://www.abc.mno/correct/
        )
    [D] => Array
        (
            [Degree] => http://www.abc.mno/degree/
            [Direct] => http://www.abc.mno/direct/
        )
)
4

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Hi Nick, thanks for the answer. It looks great. Thanks for the help. I have one question related to the fiddle. Let's say listing have some accented letters. In the fiddle https://3v4l.org/fnMp4, let's say for example, I have stored Apple as Àpple in array. I am wondering if there is any way, it can ignore the accented character and treat `À` as `A`. – user1950349 May 15 '20 at 03:50
  • Currently, its displaying this � in the fiddle. – user1950349 May 15 '20 at 04:02
  • @user1950349 you can convert the characters using a solution such as posted [here](https://stackoverflow.com/questions/1017599/how-do-i-remove-accents-from-characters-in-a-php-string) – Nick May 15 '20 at 04:02
  • @user1950349 it displays � because it's a multi-byte character. If you don't convert the characters before grouping, you would need to change `$groups[$title[0]][$title] = $value;` to `$groups[mb_substr($title, 0, 1)][$title] = $value;` see https://3v4l.org/nsrAh – Nick May 15 '20 at 04:03
  • I am having a look at the solution [here](https://stackoverflow.com/questions/1017599/how-do-i-remove-accents-from-characters-in-a-php-string) but the answer looks fine in the fiddle https://3v4l.org/nsrAh. Instead of `À`, I want `A` in the title character. – user1950349 May 15 '20 at 04:08
  • 1
    @user1950349 using the solution from the link you can do this: https://3v4l.org/Gb6PX – Nick May 15 '20 at 04:16
  • Hi Nick, in the fiddle the title character **A** is at the bottom. Is there any way I pull at the top ? I tried with this `foreach ($beta_lists as $title => $value) { $groups[remove_accents(mb_substr($title, 0, 1))][remove_accents($title)] = $value; }`. By doing that, it changed the `À` to `A` in the paragraph but it's still not pulling title character `A` at the top. – user1950349 May 16 '20 at 03:32
  • Hi Nick, in the fiddle the title character **A** is at the bottom. Is there any way I can pull at the top ? I tried with this `foreach ($beta_lists as $title => $value) { $groups[remove_accents(mb_substr($title, 0, 1))][remove_accents($title)] = $value; }`. By doing that, it changed the `À` to `A` in the paragraph but it's still not pulling title character `A` at the top. – user1950349 May 16 '20 at 03:48
  • 1
    @user1950349 it's because they're put into the array in the order from the original array and `À` sorts after all the normal letters. Just use `ksort($groups);` https://3v4l.org/ecVj5 – Nick May 16 '20 at 07:34