0

好的,我的代码有问题,但我找不到问题所在。使用我的代码时出现此错误:

警告:array_multisort() [function.array-multisort]:第 1506 行 /home/sagesca1/public_html/hiscores/functions.ws 中的数组大小不一致

这是我的代码:

function findFriends($table){
    include "mysql.ws";
    $user = $_SESSION['username'];
    $xptype = findType($table)."xp";
    $query = mysql_query("SELECT u.* FROM friends uf inner join users u on uf.friend = u.username WHERE uf.user = '$user'") or die(mysql_error());
    $myFriends = array();
    $xpOrder = array();
    WHILE($row = mysql_fetch_array($query)){
        $count = count($row);
        $friends = $row['username'];
        $rank = findRank($friends, "$table");
        $sql = mysql_query("SELECT * FROM skills WHERE playerName LIKE '$friends' ORDER BY '$xptype' DESC") or die(mysql_error());
        while($skillsRow = mysql_fetch_array($sql)){
            for ($i = 1; $i <= $count; $i++){
                $myFriends[$friends] = array(
                    'xp' => $skillsRow[$xptype],
                    'lvl' => getLevelForXP($skillsRow[$xptype]),
                    'rank' => $rank,
                );
            $xpOrder[] = $skillsRow[$xptype];
            }
            array_multisort($xpOrder, SORT_NUMERIC, SORT_DESC, $myFriends);
            unset($xpOrder);
            foreach ($myFriends as $friend => $data){
                if(dots($data['xp']) !=0){
                echo '
                <a name="'.$data['rank'].'"></a>
                <a href="compare.ws?user1='.$friend.'" target="_self" class="row ">
                <span class="columnRank">
                <span>'.$data['rank'].'</span>
                </span>
                <span class="columnName">
                <span>'.BBCode($friend).'</span>
                </span>
                <span class="columnLevel">
                <span>'.$data['lvl'].'</span>
                </span>
                <span class="columnXp">
                <span>'.dots($data['xp']).'</span>
                </span>
                </a>'; 

                }
            }
        }
    }

}

谁能帮我解决这个问题,如果您需要关于我的代码的任何其他方法,我很乐意将它添加到这个问题中。

4

2 回答 2

0

如果要进行多重排序的数组具有相同数量的元素,则只能使用 array_multisort。根据错误,假设$xpOrder并且$myFriends具有导致此错误的不同数量的元素是有效的。

只需做print_r两个数组中的一个,然后尝试查看是否可以找出差异发生的位置。

检查此代码是否有效:

    function findFriends($table){
        include "mysql.ws";
        $user = $_SESSION['username'];
        $xptype = findType($table)."xp";
        $query = mysql_query("SELECT u.* FROM friends uf inner join users u on uf.friend = u.username WHERE uf.user = '$user'") or die(mysql_error());
        $myFriends = array();
        $xpOrder = array();
        WHILE($row = mysql_fetch_array($query)){
            $count = count($row);
            $friends = $row['username'];
            $rank = findRank($friends, "$table");
            $sql = mysql_query("SELECT * FROM skills WHERE playerName LIKE '$friends' ORDER BY '$xptype' DESC") or die(mysql_error());unset($myFriends);
            while($skillsRow = mysql_fetch_array($sql)){
unset($myFriends); 
                for ($i = 1; $i <= $count; $i++){
                    $myFriends[] = array(
                        'friend' => $friends,
                        'xp' => $skillsRow[$xptype],
                        'lvl' => getLevelForXP($skillsRow[$xptype]),
                        'rank' => $rank,
                    );
                    $xpOrder[] = $skillsRow[$xptype];
                }
                array_multisort($xpOrder, SORT_NUMERIC, SORT_DESC, $myFriends);
                unset($xpOrder); 
                foreach ($myFriends as $data){
                    if(dots($data['xp']) !=0){
                    echo '
                    <a name="'.$data['rank'].'"></a>
                    <a href="compare.ws?user1='.$data['friend'].'" target="_self" class="row ">
                    <span class="columnRank">
                    <span>'.$data['rank'].'</span>
                    </span>
                    <span class="columnName">
                    <span>'.BBCode($data['friend']).'</span>
                    </span>
                    <span class="columnLevel">
                    <span>'.$data['lvl'].'</span>
                    </span>
                    <span class="columnXp">
                    <span>'.dots($data['xp']).'</span>
                    </span>
                    </a>'; 

                    }
                }
            }
        }
    }
于 2012-08-20T21:29:18.700 回答
0

据我所知,$xpOrder数组将是$count长元素,而$myFriends在经过第一个循环后将是一个长元素,for因为您只是$friends一遍又一遍地覆盖元素。

于 2012-08-20T21:19:53.923 回答