You can do: $obj->Title etcetera.
Or you can turn it into an array:
$array = get_object_vars($obj);
Answer from Naftali on Stack OverflowYou can do: $obj->Title etcetera.
Or you can turn it into an array:
$array = get_object_vars($obj);
You create StdClass objects and access methods from them like so:
$obj = new StdClass;
$obj->foo = "bar";
echo $obj->foo;
I recommend subclassing StdClass or creating your own generic class so you can provide your own methods.
Turning a StdClass object into an array:
You can do this using the following code:
$array = get_object_vars($obj);
Take a look at: http://php.net/manual/en/language.oop5.magic.php http://krisjordan.com/dynamic-properties-in-php-with-stdclass
Getting stdClass Object values - PHP - SitePoint Forums | Web Development & Design Community
Get values stdClass Object PHP - Stack Overflow
php - Get stdClass Object value - Stack Overflow
stdclass - Getting values of a stdObject in php - Stack Overflow
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.
You can't use a std object as an array. But in order to get your code working just add this line:
$json = get_object_vars($json);
After this you can access it like this:
echo $json[6]->value;
The array $arr contains 1 item which is an object. You have to use the -> syntax to access it's attributes.
echo $arr[0]->slug;
Sorry, but you can do something more elegant.
foreach ($array as $obj)
{
// Here you can access to every object value in the way that you want
echo $obj->term_id;
}
If you just want to print you can use var_dump() or print_r().
var_dump($obj);
print_r($obj);
If you want an array of all properties and their values use get_object_vars().
$properties = get_object_vars($obj);
print_r($properties);
you should be able to see them using var_dump($myObject);
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
Add this to your code to get an array of all the "time" values
$times = array();
foreach ($sum_time as $value) {
$times[] = $value[0]->time;
}
$sum_time = array();
$time = array();
$count=0;
foreach ($a as $f) {
$sum_time[] = DB::select("select time from quiz where '$f'=id");
$time[]=$sum_time[$count]->time;
$count++;
}
print_r($time)
var_dump($sum_time);
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;
Its an object array, so just as you call the array element,
echo $stringResult["return"]->statusCode