How do I get the value from object(stdClass)?
You can do: $obj->Title
etcetera.
Or you can turn it into an array:
$array = get_object_vars($obj);
Get values stdClass Object PHP
You could do:
echo $yourObject->duration;
Get value of array stdClass Object
Each element in the "charges" array is an object. A different syntax is used to refer to the members of the object:
//for an array
echo $someArray['someValue'];
//for an object
echo $someObject->someValue;
echo $someObject->getSomeValue()
Therefore, this is the way for you:
echo $return->data->charges[0]->code;
Or, step by step:
var_dump($return);
var_dump($return->data);
var_dump($return->data->charges);
var_dump($return->data->charges[0]);
var_dump($return->data->charges[0]->code);
var_dump($return->data->charges[0]->code->billetDetails);
var_dump($return->data->charges[0]->code->billetDetails->bankAccount);
Get value from STDClass
You cannot directly get a value from $query because at this point you are just generating a query and you will get the result from $query only after executing it which you are doing at
$row=$query->result();
Looking at your result you are getting a result as an array of a stdClass object so need to json-encode your object and then decode it back to an array
$array = json_decode(json_encode($row), True);
If you are sure you will get only one row then no need for loop and you can simply do it by
echo $array[0]->login_id;
otherwise, you have to go for a loop
foreach ($array as $value) {
echo $value->login_id;
}
How to fetch the values from stdClass in PHP?
Using json_decode(json_encode(), true) to simplify stdclass object to an associative array, because it is sometimes complicated to work with multiple mixed object and array in a single variable.
I just took a piece of the code at this part of condition and loop statement to make an example
if (($structure->parts[$i]->ifdisposition) && (strtolower($structure->parts[$i]->disposition) === 'inline'))
{
Here is the only part I converted using json_decode(json_encode( $stdclass ),true)
It converts the stdclass object into associative arrays
$parameters = json_decode(json_encode($structure->parts[$i]->parameters), true);
for($i =0; $i < count($parameters); $i++)
{
if(strtolower($parameters[$i]['attribute']) === 'name') {
$body_attachments[$body_number]['is_attachment'] = true;
$body_attachments[$body_number]['name'] = $parameters[$i]['name'];
}
}
End of loop
}
You can change your loop statements from object support to an array if you want to convert whole object to an associative array
you can also use this function get_object_vars( ) it also converts the object to an array but they are different with json_decode in terms with deep I have tried them both,
get_object_vars( ) only converts the parent object not the sub object values
$parameters = (object) array('0' =>
(object) array("attribute"=> "name", "value"=>"what-is-bootstrap.png" )
);
print_r($parameters);
print_r(json_decode(json_encode($parameters), true));
print_r(get_object_vars($parameters));
and the results are
stdClass Object
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => Array
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => what-is-bootstrap.png
)
)
how get value feild[ID] stdClass Object in php code
You didn't give us a name of stdClass
so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a ->
and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array
. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object
; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
Extract Values from stdclass object in php
Since you casted it as an object upon declaration, just access it like any other normal object, thru ->
arrow operator:
echo "{$sample->sname}{$sample->bselection}{$sample->bind}";
Several versions will work as well using .
or ,
:
echo $sample->sname,$sample->bselection,$sample->bind;
Related Topics
Access MySQL Field's Comments with PHP
How to Detect If a User Has Logged Out, in PHP
Tcpdf and Insert an Image Base64 Encoded
Laravel Get Route Name from Given Url
How to Make MySQL Return Utf-8
Yii2 Links Between Frontend and Backend (Advanced Template)
"Transfer-Encoding: Chunked" Header in PHP
Set Woocommerce Cart Expiration
Php/Apache: PHP Fatal Error: Call to Undefined Function MySQL_Connect()
How to Dynamically Set Table Name in Eloquent Model
Appending a Value of a Variable to a Variable Name
Can Mongodb and Its Drivers Preserve the Ordering of Document Elements
Firefox Error 'No Element Found'