PHP - 按字母顺序排序并为特定变量赋值

时间:2016-02-22 09:43:25

标签: php sorting cakephp-3.0

PHP初学者在这里。寻找更高效的代码。

我有一个名为Brands的模型,我试图在视图中按字母顺序提取,排序和显示值。

例如:

品牌'名称从字母A开始

  1. Brand1
  2. Brand2 .....等
  3. 品牌'名称从字母K开始

    1. Brand1
    2. Brand2 .....等
    3. 我可以通过以下代码执行相同的操作:

      $ brandsTable = TableRegistry :: get(' Brands');

          $brandA = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'A%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'a%');
      
          $brandK = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'K%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'k%');
      
          $brandS = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'S%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 's%');
      
          $brandT = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'T%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 't%');
      
          $brandN = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'N%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'n%');
      
          $brandH = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'H%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'h%');
      
          $brandM = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'M%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'm%');
      
          $brandY = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'Y%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'y%');
      
          $brandR = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'R%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'r%');
      
          $brandW = $brandsTable->find('all')->where([
              'deleted =' => 0
              ])-> andWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'W%'
              )-> orWhere(['brand_name_eng LIKE :key'
              ])->bind(':key', 'w%');
      
          $this->set('brandsA', $brandA);
          $this->set('brandsK', $brandK);
          $this->set('brandsS', $brandS);
          $this->set('brandsT', $brandT);
          $this->set('brandsN', $brandN);
          $this->set('brandsH', $brandH);
          $this->set('brandsM', $brandM);
          $this->set('brandsY', $brandY);
          $this->set('brandsR', $brandR);
          $this->set('brandsW', $brandW);
      

      我认为是多次调用模型/执行查询的最有效方法。尝试寻找有效的解决方案(哈希:提取/排序等)但无法获得正确的输出。非常感谢你的帮助。谢谢!

      注意:已删除的标志用于逻辑删除。

      更新 在ThinTank和ndm评论之后 很抱歉没有提到,因为它是一个日本网站,我正在寻找区分大小写的品牌'名称从

      开始

      [A,E,I,O,U],[K],[S],[T],[N],[H],[M],[Y],[R],[W]

      分别。例如:我的品牌如下

      adidas,Allison,Edox,Reebok,Tissot等

      输出应为: 品牌'名称从ア[A,E,I,O,U]开始: 阿迪达斯 佳佳 EDOX

      品牌'名称从ラ[R]开始: 锐步

      品牌'名称从タ[T]开始: 天梭 ......等等,

1 个答案:

答案 0 :(得分:2)

以下是分解它的一种方法:

$listBrandsLetter = array('A', 'K', 'S', 'T', 'N', 'H', 'M', 'Y', 'R', 'W');

$brandsTable = TableRegistry::get('Brands');

foreach($listBrandsLetter as $letter)
{
    $currentBrand = $brandsTable->find('all')->where([
                        'deleted =' => 0
                        ])-> andWhere(['brand_name_eng LIKE :key'
                        ])->bind(':key', $letter.'%'
                        )-> orWhere(['brand_name_eng LIKE :key'
                        ])->bind(':key', strtolower($letter).'%');

    $this->set('brands'.$letter, $currentBrand);
}

如果您的目标是减少因性能问题而导致的查询次数,那么请通知它。 如果表现还可以,那么只考虑因素就足够了