LoginSignup
1
1

More than 5 years have passed since last update.

PHPの配列をjsで配列として扱う

Last updated at Posted at 2018-01-16

PHPの配列をjsで配列として扱う

php側で json_encode でjsonデータを作成して表示処理に渡す。

表示側でphpタグでJSON.parse にphpタグで先程のjsonデータを表示する。
 ※js側ではテキストデータなのでクォート「''」で囲む

php
$json = json_encode(['4'=>'19:00','5'=>'19:30','6'=>'20:00']);
include('js');
js
const times = JSON.parse('<?php echo $json; ?>')
$.each(times, function(k, v){
   console.log(k+'=>'+v) 
})
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1