stdclass

stdClass Object problems

柔情痞子 提交于 2019-12-03 18:05:38
问题 I'm struggling to parse the below data using PHP. An API returns it, and I've tried various syntaxes. How do I return the data in a non-object way? Or, what's the syntax to call the data using the stdClass? Could I convert this to one data based array, or even two? I'm lost when it comes to object-based data sets. stdClass Object ( [0] => stdClass Object ( [district] => stdClass Object ( [state] => NY [number] => 29 ) ) [1] => stdClass Object ( [district] => stdClass Object ( [state] => NY

Add properties to stdClass object from another object

筅森魡賤 提交于 2019-12-03 16:48:18
问题 I would like to be able to do the following: $obj = new stdClass; $obj->status = "success"; $obj2 = new stdClass; $obj2->message = "OK"; How can I extend $obj so that it contains the properties of $obj2, eg: $obj->status //"success" $obj->message // "OK" I know I could use an array, add all properties to the array and then cast that back to object, but is there a more elegant way, something like this: extend($obj, $obj2); //adds all properties from $obj2 to $obj Thanks! 回答1: This is more

Add properties to stdClass object from another object

纵饮孤独 提交于 2019-12-03 11:20:13
I would like to be able to do the following: $obj = new stdClass; $obj->status = "success"; $obj2 = new stdClass; $obj2->message = "OK"; How can I extend $obj so that it contains the properties of $obj2, eg: $obj->status //"success" $obj->message // "OK" I know I could use an array, add all properties to the array and then cast that back to object, but is there a more elegant way, something like this: extend($obj, $obj2); //adds all properties from $obj2 to $obj Thanks! This is more along the lines of they way that you didn't want to do it.... $extended = (object) array_merge((array)$obj,

How to set attributes for stdClass object at the time object creation

萝らか妹 提交于 2019-12-03 10:29:33
问题 I want to set attribute for a stdClass object in a single statement. I don't have any idea about it. I know the following things $obj = new stdClass; $obj->attr = 'loremipsum'; It takes two statements. $obj = (object) array('attr'=>'loremipsum'); It takes single statement but it is not direct method. $obj = new stdClass(array('attr'=>'loremipsum')); It is not working. 回答1: $obj = (object) array( 'attr'=>'loremipsum' ); Actually, that's as direct as it's going to get. Even a custom constructor

PHP Foreach Arrays and objects

谁都会走 提交于 2019-12-03 09:24:59
I have an array of objects. A print_r output looks like this: Array ( [0] => stdClass Object ( [sm_id] => 1 [c_id] => 1 ) [1] => stdClass Object ( [sm_id] => 1 [c_id] => 2 ) ) I am really struggling to find a way to loop though the results and access the object elements. If anyone could give me any pointers i would be extremely grateful. Thanks in advance Use //$arr should be array as you mentioned as below foreach($arr as $key=>$value){ echo $value->sm_id; } OR //$arr should be array as you mentioned as below foreach($arr as $value){ echo $value->sm_id; } Looping over arrays and objects is a

How to set attributes for stdClass object at the time object creation

a 夏天 提交于 2019-12-03 02:02:37
I want to set attribute for a stdClass object in a single statement. I don't have any idea about it. I know the following things $obj = new stdClass; $obj->attr = 'loremipsum'; It takes two statements. $obj = (object) array('attr'=>'loremipsum'); It takes single statement but it is not direct method. $obj = new stdClass(array('attr'=>'loremipsum')); It is not working. $obj = (object) array( 'attr'=>'loremipsum' ); Actually, that's as direct as it's going to get. Even a custom constructor won't be able to do this in a single expression. The (object) cast might actually be a simple translation

Accessing StdClass value with colon :protected

时光总嘲笑我的痴心妄想 提交于 2019-12-02 20:37:02
问题 How do I access the value of stdClass with colon ":protected"? For example, I had this $obj with these result : object(Google_Service_Plus_PeopleFeed)#14 (11) { ["title"]=> string(30) "Google+ List of Visible People" ["totalItems"]=> int(4) ["collection_key:protected"]=> string(5) "items" ["data:protected"]=> array(1) { ["items"]=> array(2) { [0]=> array(7) { ["kind"]=> string(11) "plus#person" ["etag"]=> string(57) ""42gOj_aEQqJGtTB3WnOUT5yUTkI/1eNkvlfeTwXXldr9rYAvMcwM6bk"" ["objectType"]=>

How to access a member of an stdClass in PHP

此生再无相见时 提交于 2019-12-02 12:36:56
问题 I have a JSON object that I decoded, and I can't access the element because it throws an error. stdClass Object ( [error] => InvalidRegistration ) echo $device_response->error; // gives an error How would I go about accessing attributes? Below is whole array of response: stdClass Object ( [multicast_id] => 5.3301797222004E+18 [success] => 1 [failure] => 3 [canonical_ids] => 0 [results] => Array ( [0] => stdClass Object ( [message_id] => 0:1388910534147789%25796c83f9fd7ecd ) [1] => stdClass

Accessing StdClass value with colon :protected

ぃ、小莉子 提交于 2019-12-02 10:48:58
How do I access the value of stdClass with colon ":protected"? For example, I had this $obj with these result : object(Google_Service_Plus_PeopleFeed)#14 (11) { ["title"]=> string(30) "Google+ List of Visible People" ["totalItems"]=> int(4) ["collection_key:protected"]=> string(5) "items" ["data:protected"]=> array(1) { ["items"]=> array(2) { [0]=> array(7) { ["kind"]=> string(11) "plus#person" ["etag"]=> string(57) ""42gOj_aEQqJGtTB3WnOUT5yUTkI/1eNkvlfeTwXXldr9rYAvMcwM6bk"" ["objectType"]=> string(6) "person" For example, I tried to access "kind" value which is "plus#person" using these code

How PHP parse object sdtClass [closed]

旧城冷巷雨未停 提交于 2019-12-02 10:27:16
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . When I run the SOAP client, I get the following response. How can I parse this struct in php? I wanna take only NUGGETNAME value. And how can take length