Php 使用json编码
我第一次尝试使用json编码,需要了解我做错了什么 应该是这样的: {团队:[2147483647,9],[2147483647,6],[2147483647,4],[11,2147483647],[5,2147483647],[12,8],[10,3],[2147483647,7],],结果:[[0,1],[0,1],[1,0],[0,0],[0,1],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0] 但返回的数据如下所示:- {小组:[[2147483647,10,5,12,11214748364721474836472147483647],[7,32147483647,82147483647,4,6,9],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]] 代码: 在显示团队的地方,应该每两个团队关闭一个括号]?Php 使用json编码,php,arrays,json,loops,encode,Php,Arrays,Json,Loops,Encode,我第一次尝试使用json编码,需要了解我做错了什么 应该是这样的: {团队:[2147483647,9],[2147483647,6],[2147483647,4],[11,2147483647],[5,2147483647],[12,8],[10,3],[2147483647,7],],结果:[[0,1],[0,1],[1,0],[0,0],[0,1],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0] 但返回的数据如下所示:- {小组:[[2147483647
希望有人能帮忙。不要说这会解决您的问题,但它可能会帮助您理解PHP
class mustBeAClass{
protected function max($tnid){
// must have this function - can be public for outside method use
}
protected function tourn_info($typ, $tnid){
// must have this function - can be public for outside method use
}
protected function getname($arg0, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6 = 0){
/* must have this function - can be public for outside method use
notice that $arg6 is how you assign default to arguments */
}
public function getAutoCompleteData($tournID, $db){
$db->open();
$max = $this->max($tournID); $one = $this->tourn_info("1on1", $tournID);
// what is the point of this ---->>>> $total_matches = $max;
$after_matches = $max / 2;
// notice query issue below
$matches = $db->query("SELECT * FROM matches WHERE leagueID ='$tournID' && league='tourn'"));
// while loop has problems
if($matches->num_rows < 1){
die('You have no matches');
}
else{
while($row = $db->fetch_assoc()){
$clan1 = $this->getname($row['clan1'], $tournID, 'tourn', null, 1, 1, 0);
$clan2 = $this->getname($row['clan2'], $tournID, 'tourn', null, 1, 1, 1);
if($row['matchno'] <= $after_matches) {
$clan_one[] = $row['clan1'];
$clan_two[] = $row['clan2'];
$score_one[] = $row['score1'];
$score_two[] = $row['score2'];
}
}
$data = array(
'teams' => array($clan_one, $clan_two),
'results' => array($score_one, $score_two)
);
}
$matches->free(); $db->close();
return $data;
}
}
$cls = new mustBeAClass;
echo json_encode($cls->getAutoCompleteData($tournId, $db));
当然,要为$tournId使用正确的值,为$db使用新的mysqli/*args*/值。但是第一个代码不是JSON……您的类在哪里$这是指一个类。使构造函数在类中使用公共函数{}。您的代码非常混乱。变量$1、$clan1、$clan2从不使用。您正在创建一个包含两个项的数组,每个项包含两个项。您的JSON显示的内容正是如此。您是否尝试过使用JSON_encode函数?我会假设它是您的数据库输出,或者按照以下方式处理您的数据库输出,从而创建意外的输出。json_encode只是将PHP数组像json一样绘制出来。似乎您还得到了意外的新数字,等等,所以我将尝试使用var_dump打印数据库内容,并查看到底发生了什么。
class mustBeAClass{
protected function max($tnid){
// must have this function - can be public for outside method use
}
protected function tourn_info($typ, $tnid){
// must have this function - can be public for outside method use
}
protected function getname($arg0, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6 = 0){
/* must have this function - can be public for outside method use
notice that $arg6 is how you assign default to arguments */
}
public function getAutoCompleteData($tournID, $db){
$db->open();
$max = $this->max($tournID); $one = $this->tourn_info("1on1", $tournID);
// what is the point of this ---->>>> $total_matches = $max;
$after_matches = $max / 2;
// notice query issue below
$matches = $db->query("SELECT * FROM matches WHERE leagueID ='$tournID' && league='tourn'"));
// while loop has problems
if($matches->num_rows < 1){
die('You have no matches');
}
else{
while($row = $db->fetch_assoc()){
$clan1 = $this->getname($row['clan1'], $tournID, 'tourn', null, 1, 1, 0);
$clan2 = $this->getname($row['clan2'], $tournID, 'tourn', null, 1, 1, 1);
if($row['matchno'] <= $after_matches) {
$clan_one[] = $row['clan1'];
$clan_two[] = $row['clan2'];
$score_one[] = $row['score1'];
$score_two[] = $row['score2'];
}
}
$data = array(
'teams' => array($clan_one, $clan_two),
'results' => array($score_one, $score_two)
);
}
$matches->free(); $db->close();
return $data;
}
}
$cls = new mustBeAClass;
echo json_encode($cls->getAutoCompleteData($tournId, $db));