我即将从MySQL查询中正确获取此JSON,但遇到了一些困难。
$results = [];
foreach ($getbill as $row) {
$category = $district;
$building = $row['building'];
if (!isset($results[$category]))
{$results[$building] = ['category' => $building, 'values' => []]; }
$results[$category] = ['category' => $building, 'values' => []];
$results[$row['building']]['students'][] = ['lastname' => $row['slast'], 'firstname' => $row['sfirst'], 'ens' => $row['selected'], 'part' => $row['instname'], 'division' => $row['sdiv'], 'festival' => $row['sfest']];
}
echo json_encode(array_values($results));
上面的代码导出:
[{"category":"Belmont Elementary School","values":[],"students":[{"lastname":"jones","firstname":"melissa","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"smith","firstname":"melissa","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"b","firstname":"b","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"b","firstname":"b","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"chocolate","firstname":"Charlie","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"Shmow","firstname":"Joe","ens":"C","part":"Childrens Voice","division":"1","festival":"w"},{"lastname":"abrams","firstname":"Alysond","ens":"C","part":"Childrens Voice","division":"1","festival":"w"}]},{"category":"Parliament Place","values":[]},{"students":[{"lastname":"Jones","firstname":"Joe","ens":"B","part":"Trombone","division":"1","festival":"w"},{"lastname":"Smith","firstname":"Ally","ens":"B","part":"Alto Sax","division":"1","festival":"w"}]}]
它是按学校分组的,但是,学校必须在学生信息之前的开头列出。成品需要如下,但我很茫然...
{"length":8,"schools":[{"name":"Test High School","students":[{"lastname":"Smith","firstname":"Allison","ens":"Band","part":"Bb Clarinet","division":"III","festival":"West"},{"lastname":"Jones","firstname":"Derek","ens":"Band","part":"Tuba/Sousaphone","division":"III","festival":"West"},{"lastname":"Johnson","firstname":"Matthew","ens":"Band","part":"Timpani","division":"III","festival":"West"},{"lastname":"Hughley","firstname":"Elizabeth","ens":"Band","part":"French Horn","division":"II","festival":"West"}]},{"name":"Test Elementary School","students":[{"lastname":"Jones","firstname":"Emmett","ens":"Band","part":"Bb Clarinet","division":"I","festival":"West"},{"lastname":"Aaren","firstname":"Owen","ens":"Band","part":"Tuba/Sousaphone","division":"I","festival":"West"},{"lastname":"Johns","firstname":"Sonia","ens":"Band","part":"French Horn","division":"I","festival":"West"},{"lastname":"Williams","firstname":"Nathaniel","ens":"Band","part":"Bb Clarinet","division":"I","festival":"West"}]}],"bill":120}
我假设我可以使用PHP变量通过对查询的记录进行计数并乘以15来使“ Length”和“ Bill”部分正确,但是..我如何压缩所有正确的JSON用PHP创建代码?
已更新:以下原始数据
更新2:我知道了。 @Raymond的原始评论使我朝着正确的方向前进。谢谢。我必须摆脱查询的一行,并且必须手动“回显”开始和结束(长度和成本)。工作正常!谢谢大家的帮助。
foreach ($getbill as $row) {
if (!isset($results[$building])) {
$results[$building] = ['name' => $row['building']];
}
$results[$row['building']]['students'][] = ['lastname' => $row['slast'], 'firstname' => $row['sfirst'], 'ens' => $ens, 'part' => $row['instname'], 'division' => $age, 'festival' => $loc];
}
答案 0 :(得分:1)
此代码示例对您有帮助吗?
PHP对象将转换为{}
部分的JSON对象。
PHP数组将转换为JSON数组,而该数组不在[]
部分。
PHP代码
<?php
$StdClass = new StdClass();
$StdClass->length = 10;
$StdClass->schools = array_values(array(1, 2));
var_dump(json_encode($StdClass));
?>
结果
string(29) "{"length":10,"schools":[1,2]}"
已编辑,因为未注释:
感谢您的快速回复,但很遗憾,我真的很难受 时间弄清楚了。还有,所有这些“字符” 算不上我。
是的,为学校和学生获取[{..}]
JSON格式比较棘手。
我建议您编写自定义类,并使用JsonSerializable将自定义对象转换为JSON
PHP 7代码
<?php
class school implements JsonSerializable {
private $name = null;
private $students = array();
private function __construct() {
}
public function __destruct() {
$this->name = null;
$this->students = null;
}
// Use a static object because it will enable method chaining
public static function getInstance() {
return new school();
}
public function setName(string $name) : school {
$this->name = $name;
return $this; // needed to chain
}
public function setStudent(Student $student) : school {
$this->students[] = $student;
return $this; // needed to chain
}
public function getName() : ?string {
return $this->name;
}
public function getStudents() : ?array {
return $this->students;
}
public function __get($name) : mixed {
return $this->name;
}
public function jsonSerialize() {
return array(
"name" => $this->name
, "students" => $this->students
);
}
}
class student implements JsonSerializable {
private $lastname = null;
private $firstname = null;
private function __construct() {
}
public function __destruct() {
$this->lastname = null;
$this->firstname = null;
}
// Use a static object because it will enable method chaining
public static function getInstance() {
return new student();
}
public function setLastName(string $lastname) : student {
$this->lastname = $lastname;
return $this; // needed to chain
}
public function setFirstName(string $firstname) : student {
$this->firstname = $firstname;
return $this; // needed to chain
}
public function getFirstName() : ?string {
return $this->firstname;
}
public function getLastName() : ?string {
return $this->lastname;
}
public function jsonSerialize() {
return array(
"lastname" => $this->lastname
, "firstname" => $this->firstname
);
}
}
$json = new stdClass();
$json->length = 10;
$json->schools = array(
school::getInstance()
->setName('Test High School')
->setStudent(
student::getInstance()
->setFirstname('Smith')
->setLastname('Allison')
)
->setStudent(
student::getInstance()
->setFirstname('Jones')
->setLastname('Derek')
)
,
school::getInstance()
->setName('Test Elementary School')
->setStudent(
student::getInstance()
->setFirstname('Jones')
->setLastname('Emmett')
)
);
var_dump(json_encode($json));
?>
p.s 要使代码在较低的PHP版本上运行,请删除PHP的return type declarations
采用这种JSON格式的结果
{
"length": 10,
"schools": [{
"name": "Test High School",
"students": [{
"lastname": "Allison",
"firstname": "Smith"
}, {
"lastname": "Derek",
"firstname": "Jones"
}]
}, {
"name": "Test Elementary School",
"students": [{
"lastname": "Emmett",
"firstname": "Jones"
}]
}]
}
答案 1 :(得分:0)
为了使您的生活更轻松,我建议您像
$students_array = [];
array_push($students_array, ["key"=>"data"]);
要获取与您想要的数组相似的数组,请查看下面的结构。 以此类推,对于嵌套的嵌套,我认为这会有所帮助,不要犹豫,问!
json_encode(
[
"length"=>/*sizeof($some_array)*/ 2,
"schools" =>
[
"name" => 'test highscool',
"students" => [
[
"lastname" => "Smith",
"firstname" => "Allison",
"ens" => "Band",
"part" => "Bb Clarinet",
"division" => "III",
"festival" => "West"],
[
"lastname" => "Jones",
"firstname" => "Derek",
"ens" => "Band",
"part" => "Tuba/Sousaphone",
"division" => "III",
"festival" => "West"
]
]
],
[
"name" => 'test highscool_2',
"students" => [
[
"lastname" => "Smith 2",
"firstname" => "Allison 2",
"ens" => "Band 2",
"part" => "Bb Clarinet 2",
"division" => "III 2",
"festival" => "West 2"]
]
]
]
);