Question

I have an array of data that contains the alphabetically sorted data, but in view I have different sections. Each alphabet represents each section. I need to list the data in such order that section with A must contain all the names starting with A, section B must contain the names sarting with B and so on.

My work out I have made an array that gives me the data separates the data with there starting alphabets

foreach ($data['stores_cat_data'] as $store ) {
     $store_title   =   $store['title']."<br />";
    $curr   =   current(str_split($store_title));

    if(!preg_match("/^[a-zA-Z]$/", $curr))
    {
        $storeArray['0-9'][$store_title] = $store;
    }
    else{
        $storeArray[$curr][$store_title] = $store;
    }

}

EDITED my output is

array(46) {
  ["0-9"]=>
  array(21) {
    ["1 800 Lighting
"]=>
    array(7) {
      ["title"]=>
      string(14) "1 800 Lighting"
      ["cCommisions_percentage"]=>
      string(5) "8.00%"
      ["url"]=>
      string(49) "http://www.gopjn.com/t/S0BHSUVJQERDREpHSUBHS0xKRg"
      ["id"]=>
      string(3) "539"
      ["logo"]=>
      string(14) "1389213829.gif"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["1-800 CONTACTS
"]=>
    array(7) {
      ["title"]=>
      string(14) "1-800 CONTACTS"
      ["cCommisions_percentage"]=>
      string(73) "2.4%(Repeat Customer Repeat Sale) and 7.2%(New Customer First Time Sale) "
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.274994&fot=9999&foc=1"
      ["id"]=>
      string(3) "598"
      ["logo"]=>
      string(14) "1389289817.jpg"
      ["publisher_id"]=>
      string(1) "4"
      ["subid"]=>
      string(13) "&fobs={subid}"
    }
    ["1-800-Bakery 
"]=>
    array(7) {
      ["title"]=>
      string(13) "1-800-Bakery "
      ["cCommisions_percentage"]=>
      string(30) "10.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=266793&type=3&subid=0"
      ["id"]=>
      string(2) "22"
      ["logo"]=>
      string(14) "1387403703.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["1-800-BASKETS.COM
"]=>
    array(7) {
      ["title"]=>
      string(17) "1-800-BASKETS.COM"
      ["cCommisions_percentage"]=>
      string(5) "8.00%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.216994&fot=9999&foc=1"
      ["id"]=>
      string(3) "599"
      ["logo"]=>
      string(14) "1389289896.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["1-800-FLOWERS.COM
"]=>
    array(7) {
      ["title"]=>
      string(17) "1-800-FLOWERS.COM"
      ["cCommisions_percentage"]=>
      string(6) "10.40%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.225130&fot=9999&foc=1"
      ["id"]=>
      string(4) "1659"
      ["logo"]=>
      string(14) "1390516089.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["1-800-PetMeds
"]=>
    array(7) {
      ["title"]=>
      string(13) "1-800-PetMeds"
      ["cCommisions_percentage"]=>
      string(42) "12.00%-16.00% commission on selected goods"
      ["url"]=>
      string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=56753&type=3&subid=0"
      ["id"]=>
      string(2) "24"
      ["logo"]=>
      string(14) "1387403850.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["100 Percent Pure
"]=>
    array(7) {
      ["title"]=>
      string(16) "100 Percent Pure"
      ["cCommisions_percentage"]=>
      string(29) "8.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=270135&type=3&subid=0"
      ["id"]=>
      string(2) "25"
      ["logo"]=>
      string(14) "1387404086.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["123Inkjets.com 
"]=>
    array(7) {
      ["title"]=>
      string(15) "123Inkjets.com "
      ["cCommisions_percentage"]=>
      string(41) "25.6% and 4%(OEM and Office Supply items)"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.217798&fot=9999&foc=1"
      ["id"]=>
      string(3) "596"
      ["logo"]=>
      string(14) "1389289585.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["123Print
"]=>
    array(7) {
      ["title"]=>
      string(8) "123Print"
      ["cCommisions_percentage"]=>
      string(5) "6.40%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.276533&fot=9999&foc=1"
      ["id"]=>
      string(3) "597"
      ["logo"]=>
      string(14) "1389289736.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["191 Unlimited
"]=>
    array(7) {
      ["title"]=>
      string(13) "191 Unlimited"
      ["cCommisions_percentage"]=>
      string(5) "5.00%"
      ["url"]=>
      string(49) "http://www.pntrs.com/t/S0BLS0ZGQERDREpHSUBKS0ZGSg"
      ["id"]=>
      string(3) "540"
      ["logo"]=>
      string(14) "1389214452.png"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["2020ave.com
"]=>
    array(7) {
      ["title"]=>
      string(11) "2020ave.com"
      ["cCommisions_percentage"]=>
      string(6) "10.00%"
      ["url"]=>
      string(50) "http://www.pjtra.com/t/TEFNSkZIQUVERUtISkFFREZHSks"
      ["id"]=>
      string(3) "542"
      ["logo"]=>
      string(14) "1389214732.png"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["24 Hour Fitness
"]=>
    array(7) {
      ["title"]=>
      string(15) "24 Hour Fitness"
      ["cCommisions_percentage"]=>
      string(8) "4% to 8%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.256270&fot=9999&foc=1"
      ["id"]=>
      string(3) "601"
      ["logo"]=>
      string(14) "1389290288.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["2bstores.com
"]=>
    array(7) {
      ["title"]=>
      string(12) "2bstores.com"
      ["cCommisions_percentage"]=>
      string(5) "5.00%"
      ["url"]=>
      string(51) "http://www.pntrac.com/t/TEFNSUZMQUVERUtISkFFRERKR0s"
      ["id"]=>
      string(3) "543"
      ["logo"]=>
      string(14) "1389214901.png"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["360training 
"]=>
    array(7) {
      ["title"]=>
      string(12) "360training "
      ["cCommisions_percentage"]=>
      string(6) "12.00%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.737181&fot=9999&foc=1"
      ["id"]=>
      string(3) "602"
      ["logo"]=>
      string(14) "1389290350.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["4 All Memory
"]=>
    array(7) {
      ["title"]=>
      string(12) "4 All Memory"
      ["cCommisions_percentage"]=>
      string(29) "8.00% commission on all goods"
      ["url"]=>
      string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=99169&type=3&subid=0"
      ["id"]=>
      string(2) "28"
      ["logo"]=>
      string(14) "1387404383.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["4 Wheel Drive Hardware
"]=>
    array(7) {
      ["title"]=>
      string(22) "4 Wheel Drive Hardware"
      ["cCommisions_percentage"]=>
      string(5) "5.60%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730591&fot=9999&foc=1"
      ["id"]=>
      string(3) "603"
      ["logo"]=>
      string(14) "1389290418.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["4 Wheel Parts 
"]=>
    array(7) {
      ["title"]=>
      string(14) "4 Wheel Parts "
      ["cCommisions_percentage"]=>
      string(5) "4.80%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730526&fot=9999&foc=1"
      ["id"]=>
      string(3) "604"
      ["logo"]=>
      string(14) "1389290467.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["4imprint
"]=>
    array(7) {
      ["title"]=>
      string(8) "4imprint"
      ["cCommisions_percentage"]=>
      string(5) "3.00%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.161896&fot=9999&foc=1"
      ["id"]=>
      string(3) "605"
      ["logo"]=>
      string(14) "1389290519.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["525 America
"]=>
    array(7) {
      ["title"]=>
      string(11) "525 America"
      ["cCommisions_percentage"]=>
      string(5) "6.00%"
      ["url"]=>
      string(49) "http://www.pntra.com/t/S0BLSkZKQERDREpHSUBKQ0dFRw"
      ["id"]=>
      string(3) "544"
      ["logo"]=>
      string(14) "1389215113.jpg"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["60 Minute Payday 
"]=>
    array(7) {
      ["title"]=>
      string(17) "60 Minute Payday "
      ["cCommisions_percentage"]=>
      string(4) "$48 "
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.164179&fot=9999&foc=1"
      ["id"]=>
      string(3) "606"
      ["logo"]=>
      string(14) "1389290591.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["7 For All Mankind
"]=>
    array(7) {
      ["title"]=>
      string(17) "7 For All Mankind"
      ["cCommisions_percentage"]=>
      string(29) "4.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256004&type=3&subid=0"
      ["id"]=>
      string(2) "29"
      ["logo"]=>
      string(14) "1387404461.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
  }
  ["A"]=>
  array(83) {
    ["A.N.S.I
"]=>
    array(7) {
      ["title"]=>
      string(7) "A.N.S.I"
      ["cCommisions_percentage"]=>
      string(30) "10.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=208750&type=3&subid=0"
      ["id"]=>
      string(2) "48"
      ["logo"]=>
      string(14) "1387408300.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["ABC Distributing LLC
"]=>
    array(7) {
      ["title"]=>
      string(20) "ABC Distributing LLC"
      ["cCommisions_percentage"]=>
      string(69) "4.00% commission on all goods; [2.00% commission on selected goods]; "
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=238767&type=3&subid=0"
      ["id"]=>
      string(2) "30"
      ["logo"]=>
      string(14) "1387404636.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["ABCmouse.com
"]=>
    array(7) {
      ["title"]=>
      string(12) "ABCmouse.com"
      ["cCommisions_percentage"]=>
      string(27) "$4.00 flat fee on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256295&type=3&subid=0"
      ["id"]=>
      string(2) "31"
      ["logo"]=>
      string(14) "1387405955.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["Abe's of Maine 
"]=>
    array(7) {
      ["title"]=>
      string(15) "Abe's of Maine "
      ["cCommisions_percentage"]=>
      string(5) "4.00%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.290656&fot=9999&foc=1"
      ["id"]=>
      string(3) "607"
      ["logo"]=>
      string(14) "1389290715.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["Abhair
"]=>
    array(7) {
      ["title"]=>
      string(6) "Abhair"
      ["cCommisions_percentage"]=>
      string(37) "12.00%-17.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=280480&type=3&subid=0"
      ["id"]=>
      string(2) "33"
      ["logo"]=>
      string(14) "1387406425.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["About Airport Parking
"]=>
    array(7) {
      ["title"]=>
      string(21) "About Airport Parking"
      ["cCommisions_percentage"]=>
      string(6) "50.00%"
      ["url"]=>
      string(49) "http://www.pntrs.com/t/S0BMRUVFQERDREpHSUBMRUpHSA"
      ["id"]=>
      string(3) "560"
      ["logo"]=>
      string(14) "1389224403.jpg"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["Absolute LoJack 
"]=>
    array(7) {
      ["title"]=>
      string(16) "Absolute LoJack "
      ["cCommisions_percentage"]=>
      string(73) "$1.00 flat fee on selected goods; [15.00% commission on selected goods]; "
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=274953&type=3&subid=0"
      ["id"]=>
      string(2) "34"
      ["logo"]=>
      string(14) "1387406520.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["Abt.com
"]=>
    array(7) {
      ["title"]=>
      string(7) "Abt.com"
      ["cCommisions_percentage"]=>
      string(5) "3.00%"
      ["url"]=>
      string(50) "http://www.pjatr.com/t/TEFNSUtIQUVERUtISkFFREVIREc"
      ["id"]=>
      string(3) "547"
      ["logo"]=>
      string(14) "1389215949.jpg"
      ["publisher_id"]=>
      string(1) "3"
      ["subid"]=>
      string(12) "&sid={subid}"
    }
    ["Accessorize
"]=>
    array(7) {
      ["title"]=>
      string(11) "Accessorize"
      ["cCommisions_percentage"]=>
      string(29) "5.00% commission on all goods"
      ["url"]=>
      string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=215113&type=3&subid=0"
      ["id"]=>
      string(4) "1660"
      ["logo"]=>
      string(14) "1390518973.jpg"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
    ["AccessoryGeeks.com 
"]=>
    array(7) {
      ["title"]=>
      string(19) "AccessoryGeeks.com "
      ["cCommisions_percentage"]=>
      string(6) "13.00%"
      ["url"]=>
      string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.227987&fot=9999&foc=1"
      ["id"]=>
      string(3) "608"
      ["logo"]=>
      string(14) "1389291008.gif"
      ["publisher_id"]=>
      string(1) "1"
      ["subid"]=>
      string(11) "&u1={subid}"
    }
Was it helpful?

Solution

This should do it:

$storeArray = array();
foreach ($data['stores_cat_data'] as $store) {
    $title = $store['title'];
    $first_char = substr($title, 0, 1);

    if (preg_match("/^[0-9]$/", $first_char)) {
        $key = "0-9";
    } else {
        $key = strtoupper($first_char);
    }
    if (!isset($storeArray[$key])) {
        $storeArray[$key] = array();
    }
    $storeArray[$key][$title] = $store;
}
var_dump($storeArray);

Your code looks almost correct. The way you determine $curr is a little strange and you have to account for uppercase/lowercase. Also, to avoid array warnings make sure you initialize each letter of the alphabet, as you go.

You might also want to remove all non-alphanumeric characters, like " and - that might mess up the detection. A title of "Best" book shop will be put under 0-9 where B would be better.

A regexp like: preg_replace("/[^a-zA-Z0-9]/", "", $str) should do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top