NotionCommotion Posted August 9, 2016 Share Posted August 9, 2016 Is there a "better" way to do this? Desired result is PHP equivalent to {name:"my name", data: [4,6,8]}. private function makeNameDataObject($name,array $data) { $obj = new stdClass; $obj->name=$name; $obj->data=$data; return $obj; } Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/ Share on other sites More sharing options...
Jacques1 Posted August 9, 2016 Share Posted August 9, 2016 How about an associative array? Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535832 Share on other sites More sharing options...
Barand Posted August 9, 2016 Share Posted August 9, 2016 $obj = (object)[ 'name' => 'my name', 'data' => [2,4,6]]; Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535839 Share on other sites More sharing options...
NotionCommotion Posted August 9, 2016 Author Share Posted August 9, 2016 How about an associative array? This was my first approach,, however, I am using it to add to an object created by json_decode(), it it needs to be in the form I described. $obj = (object)[ 'name' => 'my name', 'data' => [2,4,6]]; That is what I had done after my first approach, but data also gets changed to an object which not desired. Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535841 Share on other sites More sharing options...
Jacques1 Posted August 9, 2016 Share Posted August 9, 2016 json_decode() does not force you to use objects. Pass true to the second parameter, and you get plain old associative arrays. Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535842 Share on other sites More sharing options...
Barand Posted August 9, 2016 Share Posted August 9, 2016 looks like data is array to me $obj = (object)[ 'name' => 'my name', 'data' => [2,4,6]]; print_r($obj); /* results ******************** stdClass Object ( [name] => my name [data] => Array ( [0] => 2 [1] => 4 [2] => 6 ) ) */ Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535843 Share on other sites More sharing options...
NotionCommotion Posted August 9, 2016 Author Share Posted August 9, 2016 json_decode() does not force you to use objects. Pass true to the second parameter, and you get plain old associative arrays. True statement, and maybe I should change my approach. I do feel, however, objects are a little more readable and concise. looks like data is array to me Why, yes it does! I swore my array got changed to an object. Must have been some other error elsewhere. Quote Link to comment https://forums.phpfreaks.com/topic/301801-creating-a-stdclass-object/#findComment-1535845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.