How to Get the Value from Object(Stdclass)

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



Leave a reply



Submit