按字母顺序将数组拆分为多维数组

Split na array to a multidimensional array alphabetically

我有一个 PHP 数组返回给我,格式如下:

[0] => Array
        (
            [mainCatID] => 2
            [mainCatCode] => cat2
            [mainCatDesc] => Acupuncture
            [mainCatAddedDate] => 2016-10-12 10:22:49
            [mainCatStatus] => active
        )

[1] => Array
    (
        [mainCatID] => 3
        [mainCatCode] => cat3
        [mainCatDesc] => Medical
        [mainCatAddedDate] => 2016-10-12 10:22:49
        [mainCatStatus] => active
    )

[2] => Array
    (
        [mainCatID] => 4
        [mainCatCode] => cat4
        [mainCatDesc] => Aids & Hiv
        [mainCatAddedDate] => 2016-10-12 10:22:49
        [mainCatStatus] => active
    )


[3] => Array
    (
        [mainCatID] => 1
        [mainCatCode] => cat1
        [mainCatDesc] => Brains
        [mainCatAddedDate] => 2016-10-12 10:22:49
        [mainCatStatus] => active
    )

我想要实现的是根据 [mainCatDesc] 第一个字母表将数组拆分为不同的字母表 "chunks"。所以结果预计如下所示:

A

针灸 艾滋病与艾滋病毒

B

头脑

M

医疗

提前感谢您的帮助!干杯!

到目前为止我只有

$con = open_connection();
$allMainCatArray = getAllMainCat($con);
close_connection($con);

echo "<pre>";
print_r($allMainCatArray);
echo "</pre>";

做这个简单的循环:

$newArray = [];//create a new array
foreach($array as $item) {
   $letter = $item['mainCatDesc'][0];//get the first letter


   if (!ctype_alpha($letter)) {//test if its letter
     $newArray['#'][] = $item['mainCatDesc'];

   } else if(count($newArray[$letter])) {//test if the letter is in the array
      $newArray[$letter][] = $item['mainCatDesc'];//save the value
   } else {
       $newArray[$letter] = [$item['mainCatDesc']];
   }
}