php二维数组根据指定键值重新排序


    /**
     * @Notes:
     * 二维数组根据指定键值重新排序
     * @Interface arr_sort
     * @author [MengShuai] [<133814250@qq.com>]
     */
    public static function arr_sort($arr,$field='id',$order='SORT_ASC'){
        $sort = array(
            'order'     => $order, //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
            'field'     => $field, //排序字段
        );
        $arrSort = array();
        foreach($arr AS $uniqid => $row){
            foreach($row AS $key=>$value){
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if($sort['order']){
            array_multisort($arrSort[$sort['field']], constant($sort['order']), $arr);
        }
        return $arr;
    }


测试数据:
$userdb = array(
		0 => array(
				'uid' => 100,
				'name' => 'Sandra Shush',
				'url' => 'urlof100'
			),
	  
		1 => array(
				'uid' => 5465,
				'name' => 'Stefanie Mcmohn',
				'pic_square' => 'urlof100'
			),

	);


调用方法:
$userdb = arr_sort($userdb,$field='uid',$order='SORT_DESC');


打印方法:
var_export($userdb);


打印结果:
array (
  0 => 
  array (
    'uid' => 5465,
    'name' => 'Stefanie Mcmohn',
    'pic_square' => 'urlof100',
  ),
  1 => 
  array (
    'uid' => 100,
    'name' => 'Sandra Shush',
    'url' => 'urlof100',
  ),
)


鼎云博客
  • 最新评论
  • 总共0条评论