stdclass

How to access stdClass variables stdClass Object([max(id)])=>64)

谁说胖子不能爱 提交于 2019-12-11 06:59:48
问题 I need the very last valid entry in a database table which would be the row with the greatest primary key. So using mysqli, my query is "SELECT MAX(id) FROM table LIMIT 1". This query returns the correct number(using print_r()) but I cannot figure out how to access it. Here is the main code. Note that the $this->link refers to class with a mysqli connection. $q="select max(id) from stones limit 1"; $qed=$this->link->query($q) or die(mysqli_error()); if($qed){ $row=$qed->fetch_object(); print

Add property to stdClass at the top of the object in php

戏子无情 提交于 2019-12-11 00:19:47
问题 When creating a object in php used to return JSON is it possible to add a property and force it to go at the top? I'd like this since the object is exposed via an API and it is nice to have ids at the top. For example: $obj = new stdClass(); $obj->name = 'John'; $obj->age = 26; $obj->id = 3645; When you json_encode() this, it turns into: { "name": "John", "age": 26, "id": 3645 } Is there a way to force id at the top of the object even though it is added last? Note, I can't simply just add id

How to access a memer variable a with symbol

走远了吗. 提交于 2019-12-10 20:22:14
问题 I have a property of a PHP stdClass which starts with an @ symbol (according to my debugger). How do I access its value? @averageBaseRate = "22.49" 回答1: Use curly braces: $obj->{'@averageBaseRate'} It's the complex (curly) syntax applied to an object. Using that syntax, you can use any name you want for the member variable. One can even put really strange Unicode characters in the name. 来源: https://stackoverflow.com/questions/19655748/how-to-access-a-memer-variable-a-with-symbol

Port StdClass data to Model

天大地大妈咪最大 提交于 2019-12-06 08:36:55
问题 The problem is the following: 1) I have millions of rows in a few tables in the database, so using Eloquent is not efficient, as I have also multiple relationships. The solution in this case was to write custom DB::raw() selects and joins to accomplish the tasks efficiently. This returns a StdClass as you may know. 2) I have 4-5 models that have quite lengthy methods, which I need to use, so a best possible solution would be to create instances of those models for each row of the StdClass,

php stdClass Object

有些话、适合烂在心里 提交于 2019-12-05 07:26:39
问题 I'm a bit lost. with print_r I get these results stdClass Object ( [distlat] => 0 [distlng] => 0 [id] => 380 ) but how can echo the results? like stdClass->distlat->$distlat ? please give me a hint. 回答1: So, echo $object->distlat; echo $object->distlng; doesn't work for you? 回答2: You are using print_r on a variable I guess, such $myVariable To access the data of your object use : echo $myVariable->distlat; echo $myVariable->distlng; echo $myVariable->id; And try using var_dump instead of

PHP Array stdClass Object - Echo Value

て烟熏妆下的殇ゞ 提交于 2019-12-04 18:12:49
问题 so I have this array saved in a variable title $arr . I want to grab or echo the value of [slug] Array ( [0] => stdClass Object ( [term_id] => 11 [name] => Community Service [slug] => community-service [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => category So i want something like this echo $arr[slug] which would then display "community-service". I am sure this is something pretty easy but i can't figure out how to grab the value out of an array stdClass and echo it on the page.

PHP Foreach Arrays and objects

天涯浪子 提交于 2019-12-04 15:11:32
问题 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 回答1: 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

How to use implode on an array of stdClass objects?

对着背影说爱祢 提交于 2019-12-04 00:53:32
I have an array of stdClass objects and I want to build a comma separated list using one specific field of all those stdClass objects. My array looks like this: $obj1 = stdClass Object ( [foo] => 4 [bar] => 8 [foo-bar] => 15 ); $obj1 = stdClass Object ( [foo] => 16 [bar] => 23 [foo-bar] => 42 ); $obj1 = stdClass Object ( [foo] => 76 [bar] => 79 [foo-bar] => 83 ); $a = array(1=>$obj1 , 2=>$obj2 , 3=>$obj3); And I want to implode on foo of all the stdClass objects in that array to create a comma separated list. So the desired result is: 4,16,76 Is there any way to do this with implode (or some

php stdClass Object

家住魔仙堡 提交于 2019-12-03 21:35:24
I'm a bit lost. with print_r I get these results stdClass Object ( [distlat] => 0 [distlng] => 0 [id] => 380 ) but how can echo the results? like stdClass->distlat->$distlat ? please give me a hint. So, echo $object->distlat; echo $object->distlng; doesn't work for you? i.am.michiel You are using print_r on a variable I guess, such $myVariable To access the data of your object use : echo $myVariable->distlat; echo $myVariable->distlng; echo $myVariable->id; And try using var_dump instead of print_r . Information is a little completer with var_dump . (You have type information with it). 来源:

Add method in an std object in php

余生长醉 提交于 2019-12-03 20:39:13
问题 Is it possible to add a method/function in this way, like $arr = array( "nid"=> 20, "title" => "Something", "value" => "Something else", "my_method" => function($arg){....} ); or maybe like this $node = (object) $arr; $node->my_method=function($arg){...}; and if it's possible then how can I use that function/method? 回答1: You cannot dynamically add a method to the stdClass and execute it in the normal fashion. However, there are a few things you can do. In your first example, you're creating a