在PHP中读取和打印JSON数组 [英] Read and print json array in php

查看:59
本文介绍了在PHP中读取和打印JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的JSON数组.我只想打印名称的值.但是我得到了未定义的索引名,并且得到了name的值.下面是我的json.

I have a JSON array like below. I want to print only the values of the name. But I am getting undefined index name and getting value of name.below is my json.

[{"docId":{"id":"57dd70252a896558e573a0c8"},"docProfile":{"name":"gowtham","gender":null,"email":null,"mobile":"7406339908"},"docLocalInfo":{"username":"gowtham","otp":934343,"newPasswordToken":null,"tempMobile":"","adminVerfiy":null},"privateInfo":{"mciNumber":null,"aadharNumber":null,"panNumber":null},"tempHospitals":[],"bankInfo":null,"signupSteps":{"accountCreated":true,"otpValidated":true},"notification":null,"hospitals":[],"address":null}]

我的代码

foreach($doc_array as $data => $mydata)
    {
          foreach($mydata as $key=>$val)
          {
              echo $val['name'];
          }
    }

如何从docProfile获取name的值?任何帮助将不胜感激

How to get the values of name from docProfile? Any help would be greatly appreciated

推荐答案

在您的foreach内部,您无需再次循环,因为docProfile是json对象数组的索引

Inside your foreach you don't need to loop again since docProfile is an index of the json object array

只需简单访问

echo $mydata['docProfile']['name'].'<br>';

所以您的foreach会是这样

so your foreach would be like this

foreach($doc_array as $data => $mydata) {
    echo $mydata['docProfile']['name'].'<br>';
}

演示

这篇关于在PHP中读取和打印JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆