user3408779
user3408779

Reputation: 997

Read and print json array in php

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}]

my code

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

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

Upvotes: 1

Views: 4660

Answers (4)

Pravin Vavadiya
Pravin Vavadiya

Reputation: 3207

Try to something Like this.

<?php
$string = '[{"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}]';

$arr = json_decode($string, true);
echo $arr[0]['docProfile']['name'];
?>

Upvotes: 1

Gunaseelan
Gunaseelan

Reputation: 2542

<?php
    $json_str='[{"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}]';

    $json_arr = (array)json_decode($json_str,true);
    foreach($json_arr as $iarr => $ia)
    {
        foreach($ia["docProfile"] as $doc => $docDetails)
        {
            if($doc =="name")
            {
                echo $ia["docProfile"]["name"];
            }
        }
    }
?>

This code gives you the answer

Upvotes: 0

Beginner
Beginner

Reputation: 4153

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

Just simple access it

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

so your foreach would be like this

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

Demo

Upvotes: 2

AAZ
AAZ

Reputation: 51

This array just have one row but if your array have more row you can use it; you need to decode JSON at first.

$doc_array =json_decode($doc_array ,true);

foreach($doc_array as $key=> $val){
     $val['docProfile']['name']  
}

Upvotes: 0

Related Questions