Doing print_r()
on my array I get the following:
Array (
[0] =>
stdClass Object
(
[id] => 25
[time] => 2014-01-16 16:35:17
[fname] => 4
[text] => 5
[url] => 6
)
)
How can I access a specific value in the array? The following code does not work because of the stdClass Object
echo $array['id'];
You have an array. A PHP array is basically a "list of things". Your array has one thing in it. That thing is a standard class. You need to either remove the thing from your array
$object = array_shift($array);
var_dump($object->id);
Or refer to the thing by its index in the array.
var_dump( $array[0]->id );
Or, if you're not sure how many things are in the array, loop over the array
foreach($array as $key=>$value)
{
var_dump($value->id);
var_dump($array[$key]->id);
}
How about something like this.
function objectToArray( $object ){
if( !is_object( $object ) && !is_array( $object ) ){
return $object;
}
if( is_object( $object ) ){
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
and call this function with your object
$array = objectToArray( $yourObject );
Try this:
echo $array[0]->id;
Try this, working fine -
$array = json_decode(json_encode($array), true);
Source: Stackoverflow.com