首页 文章

用于指定JSON输出的PHP数组

提问于
浏览
1

我刚开始使用JSON,所以这对我来说很难 . 我花了15个多小时试图配置如何在PHP中创建和填充数组以获得指定的JSON格式 .

我需要得到这个例子1

{"players":["xZero","Lux"],"points":[{"player":"xZero","points":"20"}]}

和这个例子2

{"players":["xZero","Lux"],"points":[]}

PHP将使用foreach循环从数据库填充example1中的数据 .

foreach($userdb as $users){
$output["users"][$i]=$users;
$i++;
}

这只是上半部分,我不知道如何在另一半中插入数据 . 获得类似example1的精确输出 .

例如2我也不知道如何填写它 . 我上半年与用户相识,但另一半是谜,如例1 .

如您所见,我对JSON的体验极差 . 我也不熟悉多维数组 .

在此先感谢您的帮助!

1 回答

  • 1

    根据您的JSON,您的PHP数组应如下所示:

    array("players"=>array("xZero","Lux"),"points"=>array(array("player"=>"xZero","points"=>20)))
    

    我最好的建议是填充这样的数组或使用类创建对象,然后使用 json_encode() 函数 .

相关问题