PHP 反向数组元素


php reverse array elements

我刚开始学习php,当我在WordPress中的数组中遇到问题时,我遇到了一些问题。

我得到这样的数组[0,1,2,3,4]

获取数组后,我需要像这样反转它[4,3,2,1,0]

这是我的数组代码

$gallery_images = array_values(get_children('pos_type=attachment&post_mime_type=image&post_parent=' . $gallery->ID));

您可以添加 array_reverse() 来反转数组。这将为您解决问题。

$gallery_images = array_reverse(array_values(get_children('pos_type=attachment&post_mime_type=image&post_parent=' . $gallery->ID)));

在 php 中反转标准 idexed 数组的最简单方法是使用 array_reverse 函数。
但是如果你只是要循环它,你不妨使用向后 for 循环:

for($i=count($array);$i-->0;) {
  echo $array[$i];
}  

这将以相反的方向循环数组。