PHP编码JSON作为单个数组

时间:2015-05-15 15:00:56

标签: php mysql arrays json

我相信我正在处理一个PHP页面的语法问题,但是尽管有多种方法测试,我似乎无法解决这个问题。

对于上下文,我有一个查询MySQL数据库的页面,并获取一组部门名称以及每个部门的三个服务器(Mac,Windows和Linux)的主机名。一旦有了这些数据,它就会调用一个函数,该函数使用这些主机名来获取有关每个服务器的更多特定数据集(比如通过其他MySQL查询,CURL等),并将每个部门服务器的数据添加到数组中。理想情况下,我尝试将数组打印为JSON数据,但所有JSON数据都是一个主数组的一部分。一切正常,除了JSON数据将每个数组分成它自己的实体;

{
"department": "Math",
"macOS": "10.10.3",
"macRAM": "64GB",
"windowsOS": "2012.2",
"windowsRAM": "128GB",
"linuxOS": "5.5.1",
"linuxRAM": "32GB",
}
{
"department": "Science",
"macOS": "10.9.5",
"macRAM": "64GB",
"windowsOS": "XP",
"windowsRAM": "128GB",
"linuxOS": "1.2.3",
"linuxRAM": "32GB",
}

如上所述,呈现的JSON数据没有用逗号分隔,括在括号中。我希望;

[
{
"department": "Math",
"macOS": "10.10.3",
"macRAM": "64GB",
"windowsOS": "2012.2",
"windowsRAM": "128GB",
"linuxOS": "5.5.1",
"linuxRAM": "32GB",
},
{
"department": "Science",
"macOS": "10.9.5",
"macRAM": "64GB",
"windowsOS": "XP",
"windowsRAM": "128GB",
"linuxOS": "1.2.3",
"linuxRAM": "32GB",
}
]

很抱歉这个长度,但这是我正在完成所有工作的PHP;

<?php
$data = array();
?>

<?php
$conn = mysqli_connect("localhost", "root", "root") or die('Error connecting to MySQLserver.');
mysqli_select_db($conn, "Database") or die("Failed to connect to database");
$sql = "SELECT * FROM Servers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    $department = $row['department'];
    $serverMac = $row['serverMac'];
    $serverWindows = $row['serverWindows'];
    $serverLinux = $row['serverLinux'];
    getData($department, $serverMac, $serverWindows, $serverLinux);
}
} else {
echo "Unable to connection to server.  Please check your settings.";
}
?>

<?
function getData($serverMac, $serverWindows, $serverLinux) {

//Gather Data About Server Mac
//do something with the variable $serverMac

//Gather Data About Server Windows
//do something with the variable $serverWindows

//Gather Data About Server Linux
//do something with the variable $serverLinux

//Put The Gathered Data In An Array
$data = array('department' => $department, 
'macOS' => $macOS, 
'macRAM' => $macRAM
'windowsOS' => $windowsOS,
'windowsRAM' => $windowsRAM,
'linuxOS' => $linuxOS,
'linuxRAM' => $linuxRAM);
printData($data);
}
?>

<?php
function printData($data) {
echo "<pre>";
echo json_encode($data, JSON_PRETTY_PRINT);
echo "</pre>";
}
?>

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

您希望在嵌套的php数组中存储有关服务器的所有数据。然后json_encode会照顾其他人。

<?php
$data = array();

$conn = mysqli_connect("localhost", "root", "root") or die('Error connecting to MySQLserver.');
mysqli_select_db($conn, "Database") or die("Failed to connect to database");
$sql = "SELECT * FROM Servers";
$result = $conn->query($sql);
$servers = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $department = $row['department'];
        $serverMac = $row['serverMac'];
        $serverWindows = $row['serverWindows'];
        $serverLinux = $row['serverLinux'];
        $servers[] = getData($department, $serverMac, $serverWindows, $serverLinux);
    }

    printData($servers);

} else {
    echo "Unable to connection to server.  Please check your settings.";
}

function getData($serverMac, $serverWindows, $serverLinux) {

    //Gather Data About Server Mac
    //do something with the variable $serverMac

    //Gather Data About Server Windows
    //do something with the variable $serverWindows

    //Gather Data About Server Linux
    //do something with the variable $serverLinux

    //Put The Gathered Data In An Array
    $data = array(
        'department' => $department, 
        'macOS' => $macOS, 
        'macRAM' => $macRAM
        'windowsOS' => $windowsOS,
        'windowsRAM' => $windowsRAM,
        'linuxOS' => $linuxOS,
        'linuxRAM' => $linuxRAM);

    return $data;
}

function printData($data) {
    echo "<pre>";
    echo json_encode($data, JSON_PRETTY_PRINT);
    echo "</pre>";
}
?>