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);
How to get object(stdClass) value in php?
Try this, Best way for single value
echo $objects->{"0"};
and for multiple values
foreach($objects as $val )
{
echo $val;
}
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
)
)
Get stdClass Object value
Try echo $json["6"]["value"];
But for this you have to use json_decode($myjson, true);
true, to get an array.
Because it's going to be two arrays inside each other and not an object you have to use 2 brackets.
How to get a variable value in a stdClass object?
Object properties are accessed with the ->
operator. Just do:
echo $stringResult->return->statusCode;
If you wanted an array you would access like this since the array contains an object:
$array = (array)$stringResult;
echo $array['return']->statusCode;
Related Topics
Safe Alternatives to PHP Globals (Good Coding Practices)
If You Create a Variable Inside a If Statement Is It Available Outside the If Statement
Kill MySQL Query on User Abort
How Download Big File Using PHP (Low Memory Usage)
Phpmailer - How to Remove Recipients
Call to Undefined Function Curl_Init() Even It Is Enabled in PHP7
PHP - Implement Logging Mechanism to File in Several Classes
Allow Cross Domain Ajax Requests
One Form with Two Submit Buttons and Different Actions for Each Button
Upload Multiple Files to PHP Server Using Curl Command Line
Mysql_Connect VS MySQL_Pconnect
Reload the Page on Hitting Back Button
How to Add Commas to Numbers in PHP
PHP Import Excel into Database (Xls & Xlsx)
How to Specify the Parent Query Field from Within a Subquery in MySQL