PHP创建没有键的数组对象

时间:2018-06-11 18:51:42

标签: php arrays json mysqli

已经有一段时间了,因为我一直在搞乱PHP,但我现在需要。

我正在尝试从PHP MySQL中完成以下操作:

"holeScores": [
  {
    "type": "RoundHoleData",
    "xtraStrokes": 0,
    "strokes": 3
  },
  {
    "type": "RoundHoleData",
    "xtraStrokes": 1,
    "strokes": 5
  },
  {
    "type": "RoundHoleData",
    "xtraStrokes": 0,
    "strokes": 5
  }
]

以下是我尝试这样做的方法:

$userCard = array();

$sql = "SELECT * FROM fb_score WHERE userid=$player AND matchid=$matchid ORDER BY hole_no ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {

        $scoreInf = array(
            "type"=> "RoundHoleData",
            "xtraStrokes"=> $row["hXtra"],
            "strokes"=> $row["stroke"]
        );

        $userCard[] = $scoreInf;
    }
}



$matchResultArray = array("userid"=>$player
                      , "matchform"=>$matchformid
                      , "totScore"=>$totScore
                      , "par_score"=>$par_score
                      , "placement"=>$i
                      , "holeScores"=>$userCard
                      , "usercardInfo"=>$userCardInfo
                      );


echo json_encode($matchResultArray, 16);

这创造了这个结果:

"usercard":{  
    "0":{  
       "type":"RoundHoleData",
       "xtraStrokes":"0",
       "strokes":"4"
    },
    "1":{  
       "type":"RoundHoleData",
       "xtraStrokes":"1",
       "strokes":"5"
    },
    "2":{  
       "type":"RoundHoleData",
       "xtraStrokes":"1",
       "strokes":"5"
    }
}

无论如何我可以创建正确的json对象格式,如果是的话......怎么样?!?

任何帮助都表示赞赏,并提前感谢: - )

1 个答案:

答案 0 :(得分:2)

您正在@page @model iVoterGuide.com.Pages.IndexModel @{ Layout = null; } <!DOCTYPE html> <html ⚡> <head> <meta charset="utf-8"> <link rel="canonical" href="IndexWholePage.cshtml"> <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"/> <title>IndexWholePage</title> <!-- Structured Data –––––––––––––––––––––––––––––––––––––––––––––––––– --> <!-- JSON-LD --> <script type="application/ld+json"> { "@@context": "http://schema.org", "@@type": "NewsArticle", "headline": "page title", "datePublished": "2018-05-19 14:52", "image": [ "logo.jpg" ] } </script> <!-- Facebook --> <meta property="og:url" content="page url" /> <meta property="og:title" content="page title" /> <meta property="og:image" content="document image" /> <meta property="og:description" content="document description" /> <meta property="og:site_name" content="iVoterGuide" /> <meta property="og:type" content="article" /> <meta property="og:locale" content="en_US" /> <meta property="og:fb:app_id" content="" /> <!-- Twitter --> <meta name="twitter:site" content="@@iVoterGuide" /> <meta name="twitter:creator" content=" " /> <!-- Boilerplate Scripts/Styles –––––––––––––––––––––––––––––––––––––––––––––––––– --> <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style> <noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript> <!-- Fonts & Icons –––––––––––––––––––––––––––––––––––––––––––––––––– --> <link href="https://fonts.googleapis.com/css?family=Roboto:200,300,400,500,700" rel="stylesheet"> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> <h1>TEST BODY</h1> </body> </html> 中使用JSON_FORCE_OBJECT常量(第二个参数,16),它也将数组转换为对象。尝试删除第二个参数,改为使用json_encode ..