从多维数组创建JSON输出

时间:2014-03-18 14:50:55

标签: php json

我使用三个SQL查询的结果填充数组。然后我创建一个临时表,将所有这些数据插入其中,然后查询表,以便能够将结果作为json数组传递给我的Java应用程序(Android)。

所以

$i=0;
try {
    $stmt = $conn->prepare("SELECT APPID FROM COMMENTROOM WHERE BADGEID=? GROUP BY APPID");
    $stmt->execute(array($badgeID));
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $row2[$i][0] = $row['APPID'];
            $i++;
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        header('Location: http://' . $server_dir);
        exit();
}

for ($i=0; $i<count($row2); $i++)
{
    try {
        $stmt = $conn->prepare("SELECT APPNAME, MARKETLINK, FILENAME, USERID FROM TABLE_ADS WHERE ID=?");
        $stmt->execute(array($row2[$i][0]));
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $row2[$i][1] = $row['APPNAME'];
                $row2[$i][2] = $row['MARKETLINK'];
                $row2[$i][3] = $row['FILENAME'];
                $row2[$i][4] = $row['USERID'];
            }
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            header('Location: http://' . $server_dir);
        exit();
    }
}

for ($i=0; $i<count($row2); $i++)
{
    try {
        $stmt = $conn->prepare("SELECT BADGEID FROM REG_USERS WHERE ID=?");
        $stmt->execute(array($row2[$i][4]));
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $row2[$i][5] = $row['BADGEID'];
            }
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            header('Location: http://' . $server_dir);
            exit();
    }
}

然后最后我将$ row2数组中的数据插入到临时表中

try {
    $stmt = $conn->prepare("SELECT * FROM TEMP_COMMENTLIST");
    $stmt->execute();
        while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
            $output[] = $row;
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        header('Location: http://' . $server_dir);
        exit();
    }
print(json_encode($output));

输出看起来像

[[{"APPID":"0000000021","APPNAME":"Enhanced Email","MARKETLINK":"https:\/\/play.google.com\/store\/apps\/details?id=com.qs.enhancedemail","FILENAME":"00000000089_2013-10-23 13:26:38_Enhanced Email.png","USERID":"00000000089","BADGEID":"2626511"},{"APPID":"0000000037","APPNAME":"Mobile....

但是我觉得必须有一个比弄乱这个临时表更好的解决方案。有没有办法从$ row2数组创建SAME输出而不再有sql查询?

解: 我混合了hotzu和SoaperGEM的解决方案:

  $result = array();
try{
    $stmt = $conn->prepare("SELECT c.APPID, t.APPNAME, t.MARKETLINK, t.FILENAME, t.USERID, c.BADGEID FROM COMMENTROOM c
    INNER JOIN TABLE_ADS t ON t.ID = c.APPID 
    INNER JOIN REG_USERS r ON r.ID = t.USERID 
    WHERE c.BADGEID = ? 
    GROUP BY c.APPID");
    $stmt->execute(array($badgeID));
    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        $result[] = $row;
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    $server_dir = $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    header('Location: http://' . $server_dir);
    exit();
}


echo "[".json_encode($result)."]";
exit();

2 个答案:

答案 0 :(得分:1)

这实际上是一个SQL问题而不是其他任何问题。你以前从未在SQL中使用过JOIN吗?你需要做的就是这样:

SELECT c.APPID, t.APPNAME, t.MARKETLINK, t.FILENAME, t.USERID, r.BADGEID 
FROM COMMENTROOM c 
INNER JOIN TABLE_ADS t ON c.APPID = t.ID 
INNER JOIN REG_USERS r ON r.ID = t.USERID

答案 1 :(得分:1)

<强> EDITED

试试这个:

$result = array();
try{
    $stmt = $conn->prepare("SELECT c.APPID, t.APPNAME, t.MARKETLINK, t.FILENAME, t.USERID, r.BADGEID FROM COMMENTROOM c
    LEFT JOIN TABLE_ADS t ON t.ID = c.APPID 
    LEFT JOIN REG_USERS r ON r.ID = t.USERID 
    WHERE c.APPID = ? 
    GROUP BY c.APPID";
    $stmt->execute(array($badgeID));
    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        $result[] = $row;
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

echo json_encode($result);