请教php大神,如何提取一个json数据里的某值?

请教php大神,如何提取一个json数据里的某值?,第1张

需要准备的材料分别是:电脑、php编辑器、浏览器。

1、首先,打开php编辑器,新建php文件,例如:index.php,填充问题基础代码。

2、在index.php中,输入代码:

$b = json_decode($a)

echo $b->content->location->lat

echo ','

echo $b->content->location->lng

3、浏览器运行index.php页面,此时lng和lat的值都被打印了出来。

可以用$obj=>key 的方式直接读取  也可以先转换为数组  用遍历数组的方法读取

<?php

$json = '{"a":100,"b":200,"c":300,"d":400,"e":500}'

//首先将json字符串转换成关联数组

$arr=json_decode($json, true) 

//然后循环读取数据

foreach($arr as $item){

 echo $item

 echo "<br/>"

}

?>

运行结果:

100

200

300

400

500

header("Content-type:text/htmlcharset=utf-8")

mysql_connect('主机名','账号','密码')

mysql_select_db('数据库名')

mysql_query('set names utf8')

$sql="SELECT username,regdate FROM 表名 where username='".$_GET['USERNAME']."'"

$res=mysql_query($sql)

if($row = mysql_fetch_row($res)){

    $json = array(

        'state'=>true,

        'username'=>$row[0],

        'regdate'=>$row[1],

        'rand_num'=>date('Y/m/d H:i:s')

    )

}else{

    $json = array(

        'state'=>false,

        'rand_num'=>date('Y/m/d H:i:s')

    )

}

echo json_encode($json)

exit()

明白的人应该一眼就能看懂吧


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6776224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存