需要按其标题值按ASC或DESC顺序对数组进行排序

时间:2018-08-31 05:39:34

标签: php arrays sorting

下面是我的数组,

  SELECT badge, [AP_KDA], [AP_Match], [ADC_KDA], [ADC_Match], [TOP_KDA], [TOP_Match], 
  [Person1],[Person2],[Person3]
 FROM (
          SELECT badge, Col, Val
         FROM (
               SELECT badge, Job + '_KDA' AS Col, CAST(KDA AS nvarchar(31)) AS Val
               FROM Temp
               UNION ALL
               SELECT badge, Job + '_Match' AS Col, CAST(Match AS nvarchar(31)) AS 
                Val  
              FROM Temp
              UNION ALL
              SELECT badge, 'Person'+ CAST ((ROW_NUMBER() OVER (PARTITION BY badge 
              ORDER BY name DESC)) AS VARCHAR) AS Col, CAST(name AS VARCHAR) AS Val
               FROM Temp
           ) AS t
          ) AS tt
        PIVOT (MIN(Val) FOR Col IN ([AP_KDA], [AP_Match], [ADC_KDA], [ADC_Match], 
        [TOP_KDA], [TOP_Match], [Person1],[Person2],[Person3])
        ) AS pvt

我想要下面的输出

     Array
   (
    [0] => Array
    (
        [topic_title] => gff
        [title] => gff
    )
[1] => Array
    (
        [topic_title] => new toipic
        [title] => new toipic
    )
[2] => Array
    (
        [topic_title] => Welcome!
        [title] =>Welcome!
    )
[3] => Array
    (
        [entry_title] => sdasdad
        [title] => sdasdad
    )
[4] => Array
    (
        [event_title] => red
        [title] => red
    )
[5] => Array
    (
        [event_title] => sa
        [title] => sa          
    )
[6] => Array
    (
        [event_title] => sadasd
        [title] => sadasd
    )
[7] => Array
    (
        [event_title] => Test Event
        [title] => Test Event
    )
)

这是PHP 5.6的打印代码。

我使用usort()函数,但没有获得所需的函数。我无法通过标题键对数组进行升序排列或降序排列。

此数组是我从其他查询中获得的,此后我将其合并到一个数组中,但无法按标题值对其进行排序

4 个答案:

答案 0 :(得分:0)

usort将达到目的:

usort($array, function($x, $y) {
    return strcasecmp($x['title'], $y['title']);
});

答案 1 :(得分:0)

您可以使用array_multisort()获得所需的结果。

例如

$title = array();
foreach ($post as $key => $row)
{
    $title[$key] = $row['title'];
}
array_multisort($title, SORT_DESC, $post);

答案 2 :(得分:0)

尝试一下:

$array = Array ( Array("topic_title" => "aatoipic","title" => "new toipic"),Array("event_title" => "Test Event","title" => "Test Event"),Array("topic_title" => "Welcome!", "title" => "Welcome!"),Array("entry_title" => "sdasdad","title" => "p",),Array("event_title" => "red","title" => "k",),Array("event_title" => "sa","title" => "sa"),Array("event_title" => "sadasd","title" => "d"),Array("topic_title" => "gff", "title" => "gff"),Array( "topic_title" => "new toipic","title" => "h"));

    $arr  = $array;
    $sort = array();
    foreach($arr as $k=>$v) {
        $sort['title'][$k] = $v['title'];
    }

    array_multisort($sort['title'], SORT_ASC, $arr);

    echo "<pre>";
    print_r($arr);

输出将会出现:

Array ( [0] => Array ( [event_title] => Test Event [title] => Test Event ) [1] => Array ( [topic_title] => Welcome! [title] => Welcome! ) [2] => Array ( [event_title] => sadasd [title] => d ) [3] => Array ( [topic_title] => gff [title] => gff ) [4] => Array ( [topic_title] => new toipic [title] => h ) [5] => Array ( [event_title] => red [title] => k ) [6] => Array ( [topic_title] => aatoipic [title] => new toipic ) [7] => Array ( [entry_title] => sdasdad [title] => p ) [8] => Array ( [event_title] => sa [title] => sa ) ) 

答案 3 :(得分:0)

您需要将usortstrcasecmp一起使用:

function querySort ($x, $y) {
     return strcasecmp($x['title'], $y['title']);
}

usort($myArray, 'querySort');