如何在 foreach 语句中使用 php 和 li 进行降序排列?

问题描述 投票:0回答:1

如何在

foreach
语句中使用 php 和 li 进行降序排列?

这是我的

$data
数组:

数组 ( [0] => 数组 ( [idx] => 1 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 0000-00-00 ) [1] =>数组 ( [idx] => 2 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 0000-00-00 ) [2] => 数组 ( [idx] => 3 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 0000-00-00 ) [3] => 数组 ( [idx] => 4 [id] => 1 [mname ] => test1 [hname] => test1 [bdate] => 2024-01-01 ) [4] => 数组 ( [idx] => 5 [id] => 1 [mname] => test1 [hname] = > test1 [bdate] => 2024-01-03 ) [5] => 数组 ( [idx] => 6 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024 -01-05 ) [6] => 数组 ( [idx] => 7 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-07 ) [7] => 数组 ( [idx] => 8 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-09 ) [8] => 数组 ( [idx] = > 9 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 0000-00-00 ) [9] => 数组 ( [idx] => 10 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-20 ) [10] => 数组 ( [idx] => 11 [id] => 1 [mname] => test1 [hname ] => test1 [bdate] => 0000-00-00 ) [11] => 数组 ( [idx] => 12 [id] => 1 [mname] => test1 [hname] => test1 [bdate] = > 0000-00-00 ) [12] => 数组 ( [idx] => 13 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 0000-00-00 ) [ 13] => 数组 ( [idx] => 14 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [14] => 数组 ( [idx ] => 15 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [15] => 数组 ( [idx] => 16 [id] = > 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [16] => 数组 ( [idx] => 17 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [17] => 数组 ( [idx] => 18 [id] => 1 [mname] => test1 [hname] => test1 [bdate ] => 2024-01-22 ) [18] => 数组 ( [idx] => 19 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [19] => 数组 ( [idx] => 20 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [20] => 数组 ( [idx] => 21 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [21] => 数组 ( [idx] => 22 [id ] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [22] => 数组 ( [idx] => 23 [id] => 1 [mname] = > test1 [hname] => test1 [bdate] => 2024-01-22 ) [23] => 数组 ( [idx] => 24 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [24] => 数组 ( [idx] => 25 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01 -22 ) [25] => 数组 ( [idx] => 26 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [26] =>数组 ( [idx] => 27 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [27] => 数组 ( [idx] => 28 [id] => 1 [mname] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [28] => 数组 ( [idx] => 31 [id] => 1 [mname ] => test1 [hname] => test1 [bdate] => 2024-01-22 ) [29] => 数组 ( [idx] => 32 [id] => 1 [mname] => test1 [hname] = > test1 [bdate] => 2024-01-22 ) )

代码

    <?php
    foreach ($data as $row){
        if($row['bdate'] == '0000-00-00' || isset($printdate[$row['bdate']])){
            continue;
        }
        $printdate[$row['bdate']] = true;
        echo '<li>'.$row['bdate'].'</li>';
    }
    ?>

显示如下。

2024-01-01
2024-01-03
2024-01-05
2024-01-07
2024-01-09
2024-01-20
2024-01-22

但是我想这样显示:

2024-01-22
2024-01-20
2024-01-09
2024-01-07
2024-01-05
2024-01-03
2024-01-01

如何在 foreach 语句中使用 php 和 li 进行降序排列?

可以用php吗?或者我是否必须使用 css 来更改它?

php foreach
1个回答
0
投票

使用

usort()
按 'bdate' 降序对
$data
数组进行排序。然后,您可以使用
foreach
循环迭代排序后的数组并打印“bdate”值。

usort($data, function($a, $b) {
    return strcmp($b['bdate'], $a['bdate']);
});

$printdate = [];
foreach ($data as $row){
    if($row['bdate'] == '0000-00-00' || isset($printdate[$row['bdate']])){
        continue;
    }
    $printdate[$row['bdate']] = true;
    echo '<li>'.$row['bdate'].'</li>';
}
© www.soinside.com 2019 - 2024. All rights reserved.