PHP读取JSON数据?
赞
踩
<?php
// 生成一个PHP数组
$data = array ();
$data ["fruit"] = "apple" ;
$data ["animal"] = "tiget";
// 把PHP数组转成JSON字符串,写入文件
$json_string = json_encode( $data );
?>
<?php
// 从文件中读取数据到PHP变量
$str = file_get_contents ( 'one.json' );
// 把JSON字符串转成PHP数组,并显示
$data = json_decode( $str , true);
print_r($data);
?>
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> </style> <script type="text/JavaScript"> function test() { alert(document.getElementById("hid").value) } </script> </head> <body> <?php $data = array("fruit" => ["apple", "banana", "aa"], "animal" => "tiger"); // 定义一个数组 $data = json_encode($data); //转成json格式 ?> <form method="post" action="hh.php"> <input type="hidden" id="hid" name="hid" value="<?php print_r($data); ?>"> <input type="button" value="点击" οnclick="test()"> </form> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> </head> <body> <script> function add(array) { alert(array); } </script> <?php $arr = array('1', '2', '3', '4', '5'); $str = json_encode($arr); echo "<input type='button' name='insert' value='点击' onClick='add($str)'>"; ?> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。