在相关章节中按字母顺序列出数据

时间:2014-01-30 17:18:29

标签: php sorting alphabetical

我有一个包含按字母顺序排序的数据的数据数组,但在视图中我有不同的部分。每个字母代表每个部分。 我需要按照以下顺序列出数据: A 的部分必须包含以A开头的所有名称, B 部分必须包含以B开头的名称,依此类推。< / p>

我的工作 我已经制作了一个数组,它给我的数据分隔数据与开始字母

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 我的输出是

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}"
    }

1 个答案:

答案 0 :(得分:0)

这应该这样做:

$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);

您的代码看起来几乎正确。您确定$curr的方式有点奇怪,您必须考虑大写/小写。此外,为避免数组警告,请确保在开始时初始化字母表中的每个字母。

您可能还想删除所有非字母数字字符,例如"-可能会弄乱检测。 "Best" book shop的标题将放在0-9下,B会更好。

preg_replace("/[^a-zA-Z0-9]/", "", $str)这样的正则表达式应该这样做。