Skip to content

Instantly share code, notes, and snippets.

@hiro-matsuno
Created August 19, 2014 00:58
Show Gist options
  • Save hiro-matsuno/4a91a37abb36378a7700 to your computer and use it in GitHub Desktop.
Save hiro-matsuno/4a91a37abb36378a7700 to your computer and use it in GitHub Desktop.
PHPでJSONを作るための方法(2次元配列)
//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