如何在mysql数据库中存储JSON数据

时间:2015-04-22 17:51:09

标签: php mysql json

JSON数据

 {"level":{"Primary":"1","University":"3"},"sub":{"5":"literature","2":"bi","3":"maths"},"rate2":{"5":"20","6":"","7":"","9":"","2":"30","3":"50"},"name":"jessie","action":"test"}

我需要拆分上面的数据和三个表。

1)tbl_user -columns(name)

2)tbl_user_level-columns(级别,名称)

3)tbl_subject_rate-columns(名称,级别,主题,费率)

基本上我需要循环来存储它们。如何访问JSON数据并将它们存储在mysql中?

我对JSON完全陌生,详细的解释和帮助将非常感谢..谢谢。

编辑:

解码后我设法通过var_dump($ data)查看数组的样子。它显示如下:

array (size=5)
  'level' => 
    array (size=2)
      'Primary' => string '1' (length=1)
      'University' => string '3' (length=1)
  'sub' => 
    array (size=3)
      5 => string 'literature' (length=10)
      2 => string 'bi' (length=2)
      3 => string 'maths' (length=5)
  'rate2' => 
    array (size=6)
      5 => string '20' (length=2)
      6 => string '' (length=0)
      7 => string '' (length=0)
      9 => string '' (length=0)
      2 => string '30' (length=2)
      3 => string '50' (length=2)
  'name' => string 'jessie' (length=6)
  'action' => string 'test' (length=4)

如何访问,级别,子,rate2和名称句柄'值?

1 个答案:

答案 0 :(得分:1)

试试这个:

$data = json_decode($return, true);

foreach($value['rate2'] as $v=>$k) {
  echo $v;
  echo $k;
}