Created
August 19, 2014 00:58
-
-
Save hiro-matsuno/4a91a37abb36378a7700 to your computer and use it in GitHub Desktop.
PHPでJSONを作るための方法(2次元配列)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PHPでJSONを作る方法 | |
//格納用変数の初期化(例はmini_blogのブログ読み込みから拾ってます)。 | |
$blogReadList = array(); | |
//2次元配列用配列の初期化 | |
$i = 0; | |
//クエリーのリザルトからJSON用配列を作る。 | |
//2次元以上の配列を使う場合は2次元配列用の変数を作成する。 | |
if(!empty($result)){ | |
while($row = mysql_fetch_array($result)){ | |
$blogReadList[$i]['blogId'] = isset($row['blogId'])? $row['blogId']:""; | |
$blogReadList[$i]['blogDate'] = isset($row['blogDate'])? $row['blogDate']:""; | |
$blogReadList[$i]['blogTitle'] = isset($row['blogTitle'])? $row['blogTitle']:""; | |
$blogReadList[$i]['blogContent'] = isset($row['blogContent'])? $row['blogContent']:""; | |
$blogReadList[$i]['id'] = isset($row['id'])? $row['id']:""; | |
$i++; | |
} | |
} | |
//2次元用配列の最終値を行カウンターとして投入 | |
$blogReadList['row_count'] = $i; | |
//JSON化する | |
echo json_encode($blogReadList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment