We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,478 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

display array value in object

Hi ,

I have return type object is "$answer" and I am getting the output by ussing "echo $answer"; is :

array
  'AnswerId' => int 1
  'Comment' => string 'The license is found.OK'

  'LicenseInt' => 
         array
         'LicenseID' => string 'L005673’.                                                                                                             'Prod_ID' => int 5644
      'LicenseDate' => 
        object(DateTime)[35]
          public 'date' => string '2020-12-12 00:00:00' 
      'LicensesIssued' => int 0
      'VendorStr' => string '



  Now I am able to display the value by 

echo $answer->AnswerId;

but unable to display the value 'LicenseID' in array 'LicenseInt' or 'date' in array 'LicenseDate'.

3
Contributors
10
Replies
3 Weeks
Discussion Span
4 Months Ago
Last Updated
12
Views
Question
Answered
subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

If the snippet you've posted is for the $answer variable, then it looks to actually be an array, not an object. Do you have the actual var_dump output?

Based on the above:

echo $answer['AnswerId'];
echo $answer['LicenseInt']['licenseID'];
echo $answer['LicenseInt']['Prod_ID'];
echo $answer['LicenseInt']['LicenseDate']->date;
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

Hi ,

The displayed output using

var_dump(AnswerId);

and from your code giving the error :Fatal error: Cannot use object of type .

subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

Please post the output from var_dump($answer) or whatever your variable is called.

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

Hi ,
done this issue.
Solution is :

echo $answer->LicenseInt['LicensesSold']; 
subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0
Question Answered as of 5 Months Ago by blocblue

if the property "AnswerID" is your array, you can access is like this:

$answer->AnswerId["LicenseInt"]["LicenseID"]
gon1387
Posting Whiz in Training
233 posts since Jan 2011
Reputation Points: 32
Solved Threads: 37
Skill Endorsements: 3

Hi ,
see wich is done and which is not done marked on the right side.

object($answer)
  public 'AnswerId' => int 1                                //done 
  public 'Comment' => string 'The license is found.OK' ()   //done
  public 'LicenseInt' => 
    array
      'LicenseID' => string 'lkjljljljlkjlljljlkjlkjlj'      //done                                                                         ' (length=100)
      'Prod_ID' => int 5652                                 //done
      'LicensesSold' => int 0                               //done
      'Days' => int 0                                       //done
      'LicExpirDate' => 
        object(DateTime)[34]
          public 'date' => string '2001-01-01 00:00:00'     //not done
          public 'timezone_type' => int 3                    //not done
          public 'timezone' => string 'RPC'                 //not done           

respective syntax and output given here

echo $answer->AnswerId;   //working 
echo $answer->Comment;   //working
echo $answer->LicenceInt['LicenceID']; //working
echo $answer->LicenceInt['LicenceSold'];  //working

but 
echo $answer->LicenceInt->LicExpirDate['timezone_type'];   //not working 

because which is working is array in object but which is object in object is not working

subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

The problem is that you're accessing LicenceInt as an object when previously you've accessed it as an array.

$answer->LicenceInt['LicExpirDate']->timezone_type;
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

didn't display any error but there is no output

   in this 
   $answer->LicenceInt['LicExpirDate']->timezone_type;
   already I tried it out
subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

Did you echo the response? Have you tried to var_dump the response?

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

Yes I tried echo and tried var_dump but no output as yet

subrata_ushasi
Junior Poster
105 posts since Aug 2008
Reputation Points: 10
Solved Threads: 6
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0884 seconds using 2.68MB