This question already has an answer here:
- How can I access an array/object? 4 answers
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'];
Try this, working fine -
Try this:
To access an array member you use
$array['KEY'];
To access an object member you use
$obj->KEY;
To access an object member inside an array of objects:
$array[0]
// Get the first object in the array$array[0]->KEY
// then access its keyYou may also loop over an array of objects like so:
Think of an array as a collection of things. It's a bag where you can store your stuff and give them a unique id (key) and access them (or take the stuff out of the bag) using that key. I want to keep things simple here, but this bag can contain other bags too :)
Update (this might help someone understand better):
An array contains '
key
' and 'value
' pairs. Providing a key for an array member is optional and in this case it is automatically assigned a numeric key which starts with 0 and keeps on incrementing by 1 for each additional member. We can retrieve a 'value' from the array by it's 'key
'.So we can define an array in the following ways (with respect to keys):
First method:
The above array will be assigned numeric keys automatically. So the key assigned to red will be 0, for blue 1 and so on.
Getting values from the above array:
Second method:
Getting values from the above array:
How about something like this.
and call this function with your object
reference
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
Or refer to the thing by its index in the array.
Or, if you're not sure how many things are in the array, loop over the array