I am someone who has been trying out webdesign for around 2 months now and I have a question. So I have the following array with object:
array(1) {
[0]=>
object(WP_Post)#416 (24) {
["ID"]=>
int(36)
["post_title"]=>
string(7) "Bakuman"
}
I am trying to get the value of "ID", but am not sure how to go about referencing it.
I have tried [0]["ID"], but doesn't work.
Also: Is it possible to get the ID without mentioning the #416 number?
Tried searching for answer, but keep coming up with results that have large amounts of OOP with so much info I can't filter through to what I need. Can anyone hlpe me out?
PHP uses -> for object properties.
So in your case
echo $array[0]->ID;
should output 36
where [0] is the first element of $array which contains the WP_Post object and ID is the property containing the value you're looking for
The 0th element of your array is in fact an object, so to access its properties, you need to use the object referencing operator ->.
Try this: $array[0]->ID
Related
I got my data from Laravel database query command:
$group = DB::table('groups')->where("id", $group_id)->first();
When I var dump my data, I get:
object(stdClass)#200 (7) {
["id"]=>
int(1)
["levels_id"]=>
int(1)
["title"]=>
string(8) "Novice 1"
["description"]=>
string(11) "Lorem Ipsum"
["max_question_display"]=>
int(5)
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
I want to access the max_question_display. But when I do:
var_dump($group["max_question_display"]);
PHP returns error Cannot use object of type stdClass as array.
When I do:
var_dump($group->max_question_display);
I get:
int(5)
But I don't want the int. I only want the 5. In integer form.
If I foreach loop the $group:
foreach ($group as $t) {
echo "<pre>";
var_dump($t);
echo "</pre>";
}
I get each of the data as a single data each loop.
int(1)
int(1)
string(8) "Novice 1"
string(11) "Lorem Ipsum"
int(5)
NULL
NULL
This is obviously also not the way the result accessed that I'm looking for.
I also tried to get the first element of array, thinking that this might be an array with 1 element, but that also raise the same error.
I get it that the general answer in this site about this error is that "stdClass is not array". I have browsed several question with similar title like mine, but nothing address object that came from Laravel DB. When I read the manual on Laravel DB, I was assured that I can access the data returned like a simple dictionary / hashmap.
EDIT: Sorry, I understand my very, very newbie mistakes. No need to answer this. Thanks.
Notice the first line of your first var_dump:
object(stdClass)#200
Because you're dealing with an object, you access its properties with ->. When you do:
var_dump($group->max_question_display);
The reason you see (int) in the output is that the var_dump function shows the value type, next to the value. To access the value, do
$group->max_question_display;
If you want to see it on screen without the type, use echo
echo $group->max_question_display; // 5
stdClass is an object. You cannot use an object with array syntax to access its properties, if the class does not implement ArrayAccess interface.
As pointed out by #IbrahimLawal , var_dump outputs both the type and value. Just echoing $group->max_question_display will provide just the value
echo $group->max_question_display; // 5
In Summary: You must use arrow syntax when interacting with stdClass.
I have an object and like to retrieve the value of one or more elements from the object. Hire is one of the objects if put in a var_dump().
object(SimpleXMLElement)#13 (2) {
["#attributes"]=>
array(1) {
["name"]=>
string(5) "chain"
}
["value"]=>
string(11) "Abba Hotels"
}
I get the value but i can not get to the name.
To get the value i use for example:
echo $row->property->value
My first thought was to use:
echo $row->property->#attributes->name
, but it return as a ERROR. I try to use #attributes in a variable but that gives a NULL.
At second thought i tried to use get_object_vars() and in_array() but no luck again.
Do you guys have a idea about how i can get to the value of the "name" object?
See the docs for SimpleXMLElement:
$object->attributes()
Will give you what you need. I.e.
echo $object->attributes()->name;
It looks like you are using a property value from somewhere. If $row is the object, then you could use this I think.
$row->#attritubes['name']
Im not fully sure but thought id give it a go helping anyawy. Let me know if it works.
I am trying to access elements from a multidimensional array in an object. For example, let's assume there is some class Foo that has a variable called $phone that represents the multidimensional array.
$phone -> structure will look like
Array {
"home" Array(1) {[0] = "555-1212"},
"work" Array(2) {[0] = "555-1234", [1] = "555=5434"},
"other" Array(1) {[0] = "555=9090"}
}
Note: We can't assume we know the keys.
I can access a value by giving explicit keys, i.e.,
$foo->phone["home"][0]
The problem comes to when I don't explicitly know the keys and pull them from elsewhere.
For example if $type="phone", $subtype = "home", and I want the first entry I would expect to use:
$object->$type[$subtype][0]
to get the value, but I get an error and it doesn't think it is an array. I am not sure where the error is.
The next thing would be to add elements to the lowest level array. I assume the following would work, but doesn't:
array_push($object->$type[$subtype], $value)
This mutidimensional array would allow me to store phone numbers labelled by keys in a single nested structure. If this is overcomplicating the issue please let me know. The reason I chose this structure is because the keys can be anything the user customizes.
Thanks.
I believe this will work if you save $object->$type as it's own variable, then access that variable to go deeper in the array.
I am trying to output a set of data, and I have, due to my LEFT OUTER JOIN got multiple rows per match.
I'm using a positive look ahead to see if the next row is the same id as the first. However the return from the database class returns an array of objects.
array(7) {
[0]=> object(stdClass)#4 (8) {
["dom_id"]=>string(1) "3"
["domain"]=>string(11) "example.com"
["status"]=>string(7) "Invalid"
["expiry"]=>string(10) "2010-07-20"
["remaining"]=>string(6) "0 Days"
["rem_id"]=>NULL
["alert_type"]=>NULL
["contact"]=>NULL
}
//etc
I am getting the following error, Fatal error: Cannot use object of type stdClass as array
My code is as follows,
echo $domains[$k+1]->alert_type;
I know that I could assign the new dimension to a variable and access that as an object, but for the sake of neatness I'd rather access it directly.
Is this possible? ..and if it is, how do I approach it?
Ta
Either
fetch as array instead of stdClass or
typecast to (array) or
use a custom class that implements ArrayAccess.
Assuming that $domains is your stdClass object which you wish was an array, you could try using variable variables:
echo $domains->{$k+1}->alert_type;
The real problem though is that you have a bunch of stdClass objects. Are you storing these in a session and then reading them later, or are you neglecting to include a file or something... ?
On stream.get, I try to
echo $feeds["posts"][$i]["attachment"]["href"];
It return the URL, but, in the same array scope where "type" is located (which returns string: video, etc), trying $feeds["posts"][$i]["attachment"]["type"] returns nothing at all!
Here's an array through PHP's var_dump: http://pastie.org/930475
So, from testing I suppose this is protected by Facebook? Does that makes sense at all?
Here it's full: http://pastie.org/930490, but not all attachment/media/types has values.
It's also strange, because I can't access through [attachment][media][href] or [attachment][media][type], and if I try [attachment][media][0][type] or href, it gives me a string offset error.
["attachment"]=> array(8) {
["media"]=> array(1) {
[0]=> array(5) {
["href"]=> string(55) "http://www.facebook.com/video/video.php?v=1392999461587"
["alt"]=> string(13) "IN THE STUDIO"
["type"]=> string(5) "video"
My question is, is this protected by Facebook? Or we can actually access this array position?
Well, once the data is returned to you, it can no longer be protected by Facebook. You have full access to everything in that result as a regular data structure.
From the looks of it, there are multiple href properties throughout, so you'll want to be careful which one you're going for. $feeds["posts"][$i]["attachment"]["href"] is a valid element for some items, but $feeds["posts"][$i]["attachment"]["media"][0]["href"] is also a valid element.
There doesn't appear to be a $feeds["posts"][$i]["attachment"]["type"] element though, so that's why you're getting nothing for that particular item. There is a type inside ["attachment"]["media"][0] however, which is probably what you want.
If you are getting a string offset error when using array syntax, you've probably mixed up an element somewhere. Strings can be accessed via array syntax. For example:
$str = "string";
echo $str[1]; //echos 't'
You would get an offset warning if you tried to access an index that was larger than the string. In any case, from the looks of that output, $feeds["posts"][$i]["attachment"]["media"][0]["type"] should work.