我尝试在我的数组中获取元素的位置,但这不起作用, 这是我的代码:
<?php
function getWorldRank(){
$query = ("SELECT nickname FROM users ORDER BY score, updateDate Desc ");
mysql_query("SET NAMES 'utf8'");
//GOES TO THE DB WITH THE QUERY
$result = mysql_query($query);
//CHECKS IF THE QUERY IS LEGAL
if(!$result)
return false;
//CHECKS IF THERE ARE NO ROWS FOUND IN THE TABLE
if(mysql_num_rows($result)==0)
return false;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$returned_array[] = $row;
}
return $returned_array;
}
$a=getWorldRank();
echo $key = array_search('vvvv', $a);
echo $key = array_search('bbbb', $a);
?>
答案 0 :(得分:1)
试试这个:
function getWorldRank() {
$query = ("SELECT nickname FROM users ORDER BY score, updateDate Desc ");
mysql_query("SET NAMES 'utf8'");
//GOES TO THE DB WITH THE QUERY
$result = mysql_query($query);
// CHECKS IF THE QUERY IS LEGAL
if(!$result) {
return false;
}
// CHECKS IF THERE ARE NO ROWS FOUND IN THE TABLE
if(!mysql_num_rows($result)) {
return false;
}
$returned_array = array();
while($row = mysql_fetch_assoc($result)) {
$returned_array[] = $row['nickname'];
}
return $returned_array;
}
$a = getWorldRank();
if(!$a) {
print 'No results returned by database';
} else {
echo 'vvvv key: ' . array_search('vvvv', $a);
echo 'bbbb key: ' . array_search('bbbb', $a);
}
答案 1 :(得分:0)
您想要更改一行:
$returned_array[] = $row;
到
$returned_array[] = $row['nickname'];
然后你将返回一个数组,而不是一个数组数组。