id | num | text
1  | a   | sdfsdf
2  | a   | sdfsdf
3  | b   | sdfsdf
4  | c   | sdfsdf
5  | a   | sdfsdf
6  | b   | sdfsdf
7  | a   | sdfsdf
8  | c   | sdfsdf


我查询num = a和orderBy ID DESC并且我有:

id | num | text
7  | a   | sdfsdf
5  | a   | sdfsdf
2  | a   | sdfsdf
1  | a   | sdfsdf


我想为此计数器添加:

count | id | num | text
  4   | 7  | a   | sdfsdf
  3   | 5  | a   | sdfsdf
  2   | 2  | a   | sdfsdf
  1   | 1  | a   | sdfsdf


我用foreach PHP获取数据:

foreach ($data as $i => $dat) {
   echo $dat->id . $dat->num . $dat->text;
}


我想要一些东西:

foreach ($data as $i => $dat) {
   echo $i . $dat->id . $dat->num . $dat->text;
}


但是$ i方向相反

最佳答案

你可以这样做:

$count = count($data);
foreach ($data as $i => $dat) {
   echo ($count - $i) . $dat->id . $dat->num . $dat->text;
}

10-07 14:20