按名称对主题排序

时间:2015-05-04 02:05:25

标签: php

我有以下功能可以使用:

function listBrandTopics($params){
  self::_getParam($params,'article_path',Wrapper::basePath().'article/');

  $articles = Array();
  $first_pass = true;
  $count = 0;
  while($result['_links']['next'] || $first_pass){
    $count++;
    $first_pass = false;
    $result = $this->_apiCall(
      Array(
        'path' => '/brands/'.$params['bid'].'/articles',
        'request_param' => Array(
          'per_page' => '100',
          'page' => $count
        )
      )
    )->getBody();
    $result = json_decode($result, true);
    $articles = array_merge($articles, array_filter($result['_embedded']['entries'], array($this, '_inSupport')));
  }

  array_walk($articles, Array($this, '_articlesProcessor'));

  $topics = $this->_topicId();

  $info = Array();
  foreach($articles as $article){
    $topic_id = intval($article['topic_id']);
    foreach($topics as $topic){
      $topic_name = trim($topic['name']);
      if($topic_id == $topic['id']){
        $info[$topic_name]['id'] = $topic['id'];
        if(!is_array($info[$topic_name]['articles'])){
          $info[$topic_name]['articles'] = Array();
        }
        array_push($info[$topic_name]['articles'], $article);
        break;
      }
    }
  }
  $params['current_brand'] = self::$brand_table[$params['bid']];
  $params['the_data'] = $info;
  $params['the_data_json'] = json_encode($info);
  return $this->_getHtmlTemplate(self::_getParam($params,'template','brandTopics.phtml'),$params);
}

它在支持网站中呈现主题及其相关文章的列表。 但是,此主题列表不按字母顺序排列。

我知道$ topic ['name']是主题的名称,但是如何让它按字母顺序返回?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

使用功能ksort按键<{p}}订购$info

后:

$info = Array();
foreach($articles as $article){
    ...
}

添加:

ksort($info);

答案 1 :(得分:0)

对字母排序的数组使用简单的sort($array)

$this->cars = array("bmw", "audi", "opel", "ford");
sort($this->cars);

如果需要按顺序保持键/值关联,请使用asort($array)(使用sort_flags)。

sort - manual

asort - manual