修复数组大小不一致的错误

时间:2012-08-20 21:13:27

标签: php mysql arrays

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

  

警告: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>'; 

                }
            }
        }
    }

}

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

2 个答案:

答案 0 :(得分:0)

我可以告诉$xpOrder数组的$count元素长$myFriends,而for在完成第一个$friends循环后很长一段时间只是一遍又一遍地重写{{1}}元素。

答案 1 :(得分: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>'; 

                    }
                }
            }
        }
    }