数组的php显示顺序(排序)示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组的php显示顺序(排序)示例相关的知识,希望对你有一定的参考价值。
/* This is a very basic example of how to change the sort order of an array. This information would normally be sent back to a database table. */ $arr[27]['id'] = 27; $arr[27]['order'] = 1; $arr[27]['name'] = 'about'; $arr[38]['id'] = 38; $arr[38]['order'] = 2; $arr[38]['name'] = 'contact'; $arr[52]['id'] = 52; $arr[52]['order'] = 3; $arr[52]['name'] = 'FAQ'; $arr[44]['id'] = 44; $arr[44]['order'] = 4; $arr[44]['name'] = 'Services'; $arr[45]['id'] = 45; $arr[45]['order'] = 5; $arr[45]['name'] = 'Sitemap'; echo 'original '.'<br />'; foreach($arr as $item) { echo 'Order: '.$item['order'].' '.$item['name'].'<br />'; } $id = 44; $int_new_order = 1; $int_current_order = 4; if( $int_new_order > $arr[$id]['order'] ) { //echo 'move higher in number'; foreach($arr as $k=>$v) { if( $v['order'] > $int_current_order AND $v['order'] <= $int_new_order) { $arr[$k]['order'] = $v['order']-1; } } } else { //echo 'move lower in number'; foreach($arr as $k=>$v) { if( $v['order'] < $int_current_order AND $v['order'] >= $int_new_order) { $arr[$k]['order'] = $v['order']+1; } } } $arr[$id]['order'] = $int_new_order; echo '<br />'; echo 'final '.'<br />'; foreach($arr as $item) { echo 'Order: '.$item['order'].' '.$item['name'].'<br />'; }
以上是关于数组的php显示顺序(排序)示例的主要内容,如果未能解决你的问题,请参考以下文章