【问题标题】:Using json encode使用 json 编码
【发布时间】:2023-04-10 22:23:01
【问题描述】:

我第一次尝试使用 json 编码,需要了解我做错了什么。

应该是这样的:

{ 团队:[ [“2147483647”,“9”],[“2147483647”,“6”],[“2147483647”,“4”],[“11”,“2147483647”],[“5 ", "2147483647"],["12", "8"],["10", "3"],["2147483647", "7"], ], 结果: [ [ [[0, 1], [0, 1],[0, 1],[1, 0],[1, 0],[0, 0],[0, 0],[0, 1],], [[0, 0] ,[0, 0],[0, 0],[0, 0],], [[0, 0],[0, 0],], [[0, 0],[0, 0]] ] ] }

但是返回的数据是这样的:-

{"团队":[["2147483647","10","5","12","11","2147483647","2147483647","2147483647"],["7","3 ","2147483647","8","2147483647","4","6","9"]],"结果":[["0","0","0","0", "0","0","0","0"],["0","0","0","0","0","0","0","0"] ]}

代码:

public function getAutoCompleteData($tournID, $db)
{

    $max           = $this->max($tournID);
    $one           = $this->tourn_info("1on1", $tournID);
    $total_matches = $max;
    $after_matches = $max / 2;

    $matches = $db->query($this->select("*", "matches", "leagueID='{$tournID}' AND league='tourn'"));
    while ($row = $db->fetchAssoc($matches)) {

        $clan1 = $this->getname($row['clan1'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 0);
        $clan2 = $this->getname($row['clan2'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 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
        )
    );

    return $data;
} 

在显示团队的地方,它应该每两队关闭括号]? 希望有人能帮忙。

【问题讨论】:

  • 但是第一个代码不是JSON...
  • 你的班级在哪里? $this 指的是一个类。让构造函数在类中使用public function __construct(){}
  • 你的代码很混乱。变量 $one、$clan1、$clan2 从未使用过。您正在创建一个包含两个项目的数组,每个项目都有两个项目。正是您的 JSON 显示的内容。
  • 你试过使用json_encode()函数吗?
  • 我会假设它是您的数据库输出或您处理数据库输出的以下方式会产生意外输出。 json_encode 只是简单地将 PHP 数组绘制出来,就好像它们是 JSON 一样。看来您还得到了意想不到的新数字等,所以我会尝试使用 var_dump 打印数据库内容,看看会发生什么。

标签: php arrays json loops encode


【解决方案1】:

并不是说这会解决你的问题,但它可以帮助你理解 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 使用您的 new mysqli(/*args*/)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2021-06-06
    相关资源
    最近更新 更多