Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

How can I get the object inside an array in PHP?

Dear Experts,
I get the result like this from an API. I use PHP 7.2

How can I get the object inside an array in PHP?

[InternalMessage] => somoething [IsSuccess] => 1 [ReferenceCode] => 12345  ) ) ) 
object(stdClass)#1 (5) { ["lpo"]=> bool(true) ["ReturnCode"]=> int(1) ["ReturnData"]=> string(57) "S40021 : - something/sdf985" ["outResultToReturn"]=> array(1) { [0]=> object(stdClass)#2 (14) { ["ReturnCode"]=> int(0) ["ReturnData"]=> NULL ["Code"]=> string(1) "0" ["ErrorCode"]=> string(4) "3D00" ["Message"]=> string(9) "fghdfghe" ["Results"]=> string(0) "" ["ClientReferenceCode"]=> string(6) "413252" ["HostKey"]=> string(0) "" ["InternalMessage"]=> string(0) "" ["Last"]=> float(0) ["Process"]=> float(0) ["ReferenceCode"]=> string(8) "dgsd456" ["Status"]=> int(1) ["TransactionType"]=> int(0) } } ["outResultToContent"]=> array(1) { [0]=> object(stdClass)#3 (6) { ["FormPostContent"]=> string(1748) "

Open in new window


I get the resutl like this
$book = json_decode($result);
// access title of $book object

//I got ReturnCode like this
$ReturnCode = $book->ReturnCode;

But I don't know how to get FormPostContent

How can I get FormPostContent ? thank you
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Let's rewrite your output
object(stdClass)#1 (5) { 
  ["lpo"]=> bool(true) 
  ["ReturnCode"]=> int(1) 
  ["ReturnData"]=> string(57) "S40021 : - something/sdf985" 
  ["outResultToReturn"]=> array(1) { 
    [0]=> object(stdClass)#2 (14) { 
      ["ReturnCode"]=> int(0) 
      ["ReturnData"]=> NULL 
      ["Code"]=> string(1) "0" 
      ["ErrorCode"]=> string(4) "3D00" 
      ["Message"]=> string(9) "fghdfghe" 
      ["Results"]=> string(0) "" 
      ["ClientReferenceCode"]=> string(6) "413252" 
      ["HostKey"]=> string(0) "" 
      ["InternalMessage"]=> string(0) "" 
      ["Last"]=> float(0) ["Process"]=> float(0) 
      ["ReferenceCode"]=> string(8) "dgsd456" 
      ["Status"]=> int(1) 
      ["TransactionType"]=> int(0) 
    } 
  } 
  ["outResultToContent"]=> array(1) { 
    [0]=> object(stdClass)#3 (6) { 
      ["FormPostContent"]=> string(1748) "

Open in new window

Call your object $data. We want the Code property from the 0 index item of the outResultToReturn value (which is an array) ...
 so

$data is an object so we use -> to access its members
$data->outResultToReturn

Open in new window

outResultToReturn is an array so we use an index to find the one we want
$data->outResultToReturn[0]

Open in new window

outResultToReturn is an array of objects so we use -> to get the value we want
$data->outResultToReturn[0]->Code

Open in new window

Done
Avatar of BR

ASKER

Dear Julian, thank you so much.

I got this error.
Undefined property: stdClass::$FormPostContent

$book = json_decode($result);

echo "<pre>FormPostContent=" . $book->outResultToReturn[0]->FormPostContent; 
echo "</pre>";

Open in new window

Then you did not post the full dump of the object. The snippet you did post is incomplete indicating there is an error above.
Please post your full NOT-decoded JSON so we can see what the actual data looks like.
Avatar of BR

ASKER

Thank you Julian,
Here is the var_dump result.

string(2456) "{"f3D":true,"ReturnCode":1,"ReturnData":"T60061 :  - Başarılı:True:İşlem Başarılı/SDFS193","outResultToReturn":[{"ReturnCode":0,"ReturnData":null,"AuthCode":"0","ErrorCode":"3D00","Message":"SSS","Results":"","ClientReferenceCode":"413252","Key":"","InternalMessage":"","Last":0.0,"Process":0.0,"ReferenceCode":"RTOPL193","Status":1,"TransactionType":0}],"outResultToContent":[{"FormPostContent":"<html>....</html>","InternalMessage":"SMTH","IsSuccess":true,"ReferenceCode":"SDFE193","successUrl":"URL.php","failUrl":"URL2.php"}]}"

Open in new window

Nope I want the JSON code - not the var_dump.
Avatar of BR

ASKER

Thank you Julian Hansen,

string(2456) "{"p3D":true,"ReturnCode":1,"ReturnData":"S40021 : - Başarılı:True:İşl/193","outResultToReturn":[{"ReturnCode":0,"ReturnData":null,"AuCode":"0","ErrorCode":"3D00","Message":"İşlemde","Results":"","ClientReferenceCode":"413252","HostKey":"","InternalMessage":"","Last":0.0,"Processt":0.0,"ReferenceCode":"işl193","Status":1,"TransactionType":0}],"outResultToContent":[{"FormPostContent":"\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n  
\"eJxVUsluwjAQ/ZUo98YqdloZOO/fRv7/wBaoUucI=\"
\r\n  
\"https://........aspx\"
\r\n  
\"v7r48p6a9fef243ee0314092b766b35c66c9d0c8
\r\n \r\n
\r\n \r\n ","InternalMessage":"İş","IsSuccess":true,"ReferenceCode":"ist193","successUrl":"https://.....","failUrl":"......"}]}" 

Open in new window

@BR.
You are posting a var_dump again - it is difficult to work with that - can you post the RAW JSON.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

thank you Julian Hansen,
here is the pure Json.
{"p9D":true,"ReturnCode":1,"ReturnData":"S40021 : - Bası:True:İşl /klmoh195","outResultToReturn":[{"ReturnCode":0,"ReturnData":null,"AuthCode":"0","ErrorCode":"3D00","KlakMessage":"İşlemde","KlakResults":"","ClientReferenceCode":"413252","HostKey":"","InternalMessage":"","LastAm":0.0,"ProcessAm":0.0,"ReferenceCode":"klmoh195","Status":1,"TransactionType":0}],"outResultToContent":[{"FormPostContent":"\r\n \r\n \r\n \r\n \r\n \r\n \r\n
\r\n  
\"eJxVUttSwjAQ/ZVO3sBHXiQVWPOffTvL3wDHra6Ig==\"
\r\n  
\"https://url.....\"
\r\n  
\"7sc0ujv6400f38e39a2a41d39fe1d0c1bf975325
\r\n \r\n
\r\n \r\n ","InternalMessage":"İşlem Başarılı","IsSuccess":true,"ReferenceCode":"klmoh195","successUrl":"url.....","failUrl":"url....."}]}

Open in new window

Avatar of BR

ASKER

thank you so much Julian Hansen,
you are great.
You are must welcome.