Php 从数据行创建多维数组
我有一个包含项目条目的数据库。每个条目都有一个项目标题、日期戳、输入者和注释。我试图将这些数据格式化为JSON,用于报告和图表 我希望每个项目名称都有一个数组,数组中的每个条目都有一个数组 我试过几种方法,但运气不太好Php 从数据行创建多维数组,php,mysql,json,mysqli,Php,Mysql,Json,Mysqli,我有一个包含项目条目的数据库。每个条目都有一个项目标题、日期戳、输入者和注释。我试图将这些数据格式化为JSON,用于报告和图表 我希望每个项目名称都有一个数组,数组中的每个条目都有一个数组 我试过几种方法,但运气不太好 if ($result = $mysqli->query("SELECT * FROM project_entries")) // WHERE WEEK(date) = WEEK(current_date) ORDER BY project_name {
if ($result = $mysqli->query("SELECT * FROM project_entries"))
// WHERE WEEK(date) = WEEK(current_date) ORDER BY project_name
{
while ($row = mysqli_fetch_array($result)) {
$entry_array = array();
$row_array['project_name'] = $row['project_name'];
$comment = $row['comment'];
$entry = array (
'comment' => $comment,
);
$row_array['entries'] = $entry;
if ( !in_array($row['project_name'], $projects, false ))
{
array_push($projects, $row_array);
}
}
}
产出:
[
{
"project_name": "Logo Design",
"entries": {
"comment": "Worked on a thing"
}
},
{
"project_name": "Logo Design",
"entries": {
"comment": "Created some stuff"
}
},
虽然我想:
{
"project_name": "Logo Design",
"entries": {
"comment": "Worked on a thing",
"comment": "Created some stuff"
}
}也许是这样的?
使用项目id生成目标阵列
while ($row = mysqli_fetch_array($result) {
if (!isset($projects[$row['id']]))
{
$projects[$row['id']] = [
'project_name' => $row['project_name'],
'entries' => [],
];
}
$projects[$row['id']]['entries'][] = [
'comment' => $row['comment'],
];
}
这应该能奏效。可以将项目名称用作数组键。为了防止字符串键显示在输出数组中,可以使用数组_值将其转换为数字键
$projects = array();
while ($row = mysqli_fetch_array($result)) {
$projects[$row['project_name']]['project_name'] = $row['project_name'];
$projects[$row['project_name']]['entries'][] = array('comment' => $row['comment']);
}
echo json_encode(array_values($projects));
您以前的代码出了什么问题:
if ($result = $mysqli->query("SELECT * FROM project_entries")) {
while ($row = mysqli_fetch_array($result)) {
$entry_array = array(); // This does not appear to be used
// With each iteration of the while loop, you create a new array of
// project information (project name and entries array with one comment)
$row_array['project_name'] = $row['project_name'];
$comment = $row['comment'];
$entry = array ('comment' => $comment);
$row_array['entries'] = $entry;
// The $projects array is an array of arrays, but $row['project_name'] is
// a string. Checking if this string is in an array of arrays will always
// be false, so the array_push should always execute.
if (!in_array($row['project_name'], $projects, false )) {
// Adds the new project array to the projects array
array_push($projects, $row_array);
}
// This is not producing what you want because you are adding a new
// array to $row_array each time the loop runs
}
}
为什么我建议的代码有效:
$projects = array(); // Empty array to hold all the projects
while ($row = mysqli_fetch_array($result)) {
// Using the project name from the database as an array key in the array we are
// constructing keeps the projects unique in that array.
// The first time a new project name occurs, this will create a new sub-array
// within $projects with project_name => the new project name. This value will
// be overwritten on subsequent occurrences of the same project name.
$projects[$row['project_name']]['project_name'] = $row['project_name'];
// For each iteration, this will add a comment to the 'entries' array in the
// project array with the key $row['project_name'].
$projects[$row['project_name']]['entries'][] = array('comment' => $row['comment']);
// For example, with the first iteration of the array we create the following:
//
// $projects['Logo Design'][
// 'project_name' =>'Logo Design',
// 'entries' => [0 => ['comment' => 'Worked on a thing'] ] ]
//
// with the second iteration, the project name is overwritten (with same project name)
// and another comment array is added to the entries array
//
// $projects['Logo Design'][
// 'project_name' =>'Logo Design',
// 'entries' => [0 => ['comment' => 'Worked on a thing'],
// 1 => ['comment' => 'Created some stuff'] ] ]
}
// If you just did echo json_encode($projects), you would not get the format you want,
// because of the string keys. Try it without the array_values() to see what I mean.
echo json_encode(array_values($projects));
展示最接近你想要的东西。最好是学习如何改进代码,而不是复制和粘贴别人给你的任何东西。谢谢,这确实有效。我仍然不完全理解它是如何工作的,或者是什么原因使我以前的代码无法工作。我很难在网上找到任何有用的东西。对于像这样的数组,有什么关于站点或教程的建议吗?@JoshDerocher I对答案做了很好的解释;希望这会有帮助。至于网站的建议,应该是有帮助的。对于教程,我真的没有具体的推荐。谢谢!那对我来说就清楚了。我感谢您的帮助: