开发者

PHP: Sorting in array

开发者 https://www.devze.com 2023-02-13 09:25 出处:网络
I need help. I have this function: function getFriendsUploadActivity($user_id, $limit){ $res = $this->exec(

I need help. I have this function:

    function getFriendsUploadActivity($user_id, $limit){
    $res = $this->exec(
        "SELECT bimg.hash,bimg.image_id,bimg.user_id,bimg.time,img_v2.server,img_v2.loc,img_v2.newname,img_v2.name FROM bimg 
        LEFT JOIN img_v2 ON bimg.image_id = img_v2.id 
        WHERE bimg.user_id IN (SELECT subscription FROM subscribes WHERE subscriber = '$user_id') 
        AND img_v2.protected = '0' 
        OR bimg.user_id = '$user_id' 
        ORDER BY bimg.time DESC"
    );
    while($r = mysql_fetch_array($res)){
        $hash[$r['hash']][] = $r;
    }

    $res2 = $this->exec(
        "SELECT * FROM sets WHERE sets.user_id IN (SELECT subscription FROM subscribes WHERE subscriber = '$user_id')  
        OR sets.user_id = '$user_id'  
        ORDER BY sets.time DESC"
    );
    while($r2 = mysql_fetch_array($res2)){
        $hash[$r2['name']][] = $r2;
    }

    return $hash;
}

This function returns array like this:

Array
(
    [5ddf] => Array
        (
            [0] => Array
                (
                    [0] => 5ddf
                    [hash] => 5ddf
                    [1] => 18778
                    [image_id] => 18778
                    [2] => 4
                    [user_id] => 4
                    [3] => 1295556514
                    [time] => 1295556514
                )

        )
    [d6e4] => Array
        (
            [0] => Array
                (
                    [0] => d6e4
                    [hash] => d6e4
                    [1] => 18631
                    [image_id] => 18631
                    [2] => 4
                    [user_id] => 4
                    [3] => 1295556512
                    [time] => 1295556512
                 )
            [1] => Array
                (
                    [0] => d6e4
                    [hash] => d6e4
                    [1] => 18630
                    [image_id] => 18630
                    [2] => 4
                    [user_id] => 4
                    [3] => 1295556510
                    [time] => 1295556510
                 )
        )

    [lorem ipsum] => Array
        (
            [0] => Array
                (
                    [0] => 33
                    [id] => 33
                    [1] => lorem ipsum
                    [name] => lorem ipsum
                    [2] => 42
                    [user_id] => 42
                    [3] => 0
                    [type] => 0
                    [4] => 233324
                    [time] => 1295745642
                )

        )
   [bla bla] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [id] => 2
                    [1] => bla bla
                    [name] => bla bla
                    [2] => 4
                    [user_id] => 4
                    [3] => 0
                    [type] => 0
                    [4] => 233324
                    [time] => 1295745632
                )

        )

)

Right now the result from $res2 开发者_运维技巧is added at the end of array. I need to sort child arrays by [time] value. So in this way all arrays in my primary array will be sorted by time witch i can get from [time] value.


You can do something along these lines:

function cmp($a,$b) {
   $timea = $a[0]['time'];
   $timeb = $b[0]['time'];
   if ($timea == $timeb) 
       return 0;
   return ($timea < $timeb)? -1:1;
}
uasort($hash, 'cmp');


You can use uasort to walk through the array one by one and sort it.

http://www.php.net/manual/en/function.uasort.php


It could be done through uasort and your own comparing function:

function maxValueOfArray($array) {
    $max = NULL;

    foreach ($array as $subarray) {
        if (array_key_exists("time", $subarray)) {
            continue;
        }

        if ($max === NULL || $max < $subarray["time"]) {
            $max = $subarray["time"];
        }   
    }

    return $max;
}

function cmpFunc($a, $b) {
    $ma = maxValueOfArray($a);
    $mb = maxValueOfArray($b);

    if ($ma == $mb) {
        return 0;
    }

    return ($ma < $mb) ? -1 : 1;
}

uasort($res, 'cmpFunc');
0

精彩评论

暂无评论...
验证码 换一张
取 消