1

私は次のようなニュースの配列を持っています

Array
(
  [0] => Array
    (
        [news_published] => 1337192831
        [news_category] => 5
    )

  [1] => Array
    (
        [news_published] => 1334566743
        [news_category] => 5
    )

  [2] => Array
    (
        [news_published] => 1340092425
        [news_category] => 6
    )

  [3] => Array
    (
        [news_published] => 1339740173
        [news_category] => 6
    )

  [4] => Array
    (
        [news_published] => 1336148837
        [news_category] => 6
    )
)

news_published を降順で並べ替えるにはどうすればよいですか....「usort」で試しましたが、結果が適切に見つからないので、誰かに提案してもらえますか?

4

3 に答える 3

0
<?php


 $array = array( array('news_published'=>'1337192831','news_category'=>'5'),
            array('news_published'=>'1337192231','news_category'=>'5'),                
            array('news_published'=>'1337192921','news_category'=>'6'),

           );
  / orignal array
 print_r($array);

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

 // sorted array
print_r($array);

 ?>
于 2013-03-18T12:02:15.280 に答える
0

またはこれ:

function sortForMe($a, $b)
{
    if ((int)$a['news_published'] === (int)$b['news_published']) {
        return 0;
    }
    return (int)$a['news_published'] < (int)$b['news_published'] ? -1 : 1;
}

usort($array, 'sortForMe');

クラスから関数または静的メソッドを使用できます-あなたの選択:)

于 2013-03-18T12:05:49.957 に答える