几种方法

多维数组返回最大和最小的一条

usort($array, function($a, $b) {
  return $a['key'] - $b['key'];
});
$min = $array[0];//返回最小的一条
$max = $array[count($array)-1];//返回最大的一条

 数组变形:根据某一字段值相同的组合

Array
(
    [0] => Array
        (
            [chuangwei] => 1
            [hostel_id] => 1
        )

    [1] => Array
        (
            [chuangwei] => 6
            [hostel_id] => 2
        )

    [2] => Array
        (
            [chuangwei] => 6
            [hostel_id] => 3
        )

)

Array
(
    [0] => Array
        (
            [chuangwei] => 1
            [hostel_id] => 1
        )

    [1] => Array
        (
            [chuangwei] => 6
            [hostel_id] => 2,3
        )

)

$_tparr = array();
foreach($arr as $k=>$v){
    if(isset($_tparr[$v['chuangwei']])){
        $_tparr[$v['chuangwei']]['hostel_id'] = $_tparr[$v['chuangwei']]['hostel_id'].','.$v['hostel_id'];
    }else{
        $_tparr[$v['chuangwei']] = $v;
    }
}

 

posted @ 2016-07-02 15:34  阿虎~  阅读(175)  评论(0编辑  收藏  举报