0

this is my reply

Array ([code] => 202 [message] => Accepted [data] => Array ( [resultMap] => Array ( [D3856~H158] => Array ( [AppDay] => * [HosTown] => Colombo 06 [SpecName] => Physiotherapist/Sports Physiotherapist [HosName] => Revival Healthcare Services (Pvt)-Colombo [SpecializationId] => 333 [HosCode] => H158 [AppDate] => Any [DocName] => MR CHAMARA MATHANGAWEERA [DoctorNo] => D3856 ) ) ) [detailMessage] => Success ) 

now i want to assign variable for this vale and echo in proper way i try this but it is giving a error msg

Undefined index: DocName

this is my code ////////////////////////////////////

if( $response ){
    if ( isset($result->error) )die( $result->error_message );
    /* Convert json data to array */
    $arr=json_decode( $response, true );
    //print_r($arr);
    foreach($arr['data'] as $data)
    {
        $output="Doctor".$data['DocName']."<br/>";
        $output="Doctor".$data['SpecName']."<br/>";
        $output="Doctor".$data['HosName']."<br/>";
        $output="Doctor".$data['Day']."<br/>";
        $output="Doctor".$data['Date']."<br/>";
    }
2

2 Answers 2

2

Have a look at the structure of your array. The values you are looking for are nested withing several arrays:

$data = array (
    'code' => 202,
    'message' => 'Accepted', 
    'data' => array ( 
        'resultMap' => array ( 
            'D3856~H158' => array ( 
                'AppDay' => '*', 
                'HosTown' => 'Colombo 06',
                'SpecName' => 'Physiotherapist/Sports Physiotherapist',
                'HosName' => 'Revival Healthcare Services (Pvt)-Colombo',
                'SpecializationId' => 333,
                'HosCode' => 'H158',
                'AppDate' => 'Any',
                'DocName' => 'MR CHAMARA MATHANGAWEERA',
                'DoctorNo' => 'D3856',
            ) 
        ) 
    ),
    'detailMessage' => 'Success'
);

To print all values for the first record in resultMap:

foreach(current($data['data']['resultMap']) as $key => $value) {
    echo $key . " => " . $value . "<br />";
}

You need an additional loop in order to print all records in resultMap.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this working fine for this fixed array values means below given

foreach($arr['data']['resultMap']['D3856~H158'] as $data)
 {
        $output="Doctor".$data['DocName']."<br/>";
        $output="Doctor".$data['SpecName']."<br/>";
        $output="Doctor".$data['HosName']."<br/>";
        $output="Doctor".$data['Day']."<br/>";
        $output="Doctor".$data['Date']."<br/>";


 } 

or The changing array values are given the answer

foreach($arr['data']['resultMap'] as $data)
 {
        $output="Doctor".$data['DocName']."<br/>";
        $output="Doctor".$data['SpecName']."<br/>";
        $output="Doctor".$data['HosName']."<br/>";
        $output="Doctor".$data['Day']."<br/>";
        $output="Doctor".$data['Date']."<br/>";


 } 

1 Comment

Please take the time to add proper code highlighting to make reading easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.