本文将为您提供关于PHP:计算一个stdClass对象的详细介绍,我们还将为您解释php计算的相关知识,同时,我们还将为您提供关于PHP/JSON–stdClass对象、PHP/JSON-stdCla
本文将为您提供关于PHP:计算一个stdClass对象的详细介绍,我们还将为您解释php 计算的相关知识,同时,我们还将为您提供关于PHP / JSON – stdClass对象、PHP / JSON-stdClass对象、PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass、PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass_PHP教程的实用信息。
本文目录一览:- PHP:计算一个stdClass对象(php 计算)
- PHP / JSON – stdClass对象
- PHP / JSON-stdClass对象
- PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass
- PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass_PHP教程
PHP:计算一个stdClass对象(php 计算)
我有一个从json_decode创建的stdClass对象,当我运行count($
obj)函数时不会返回正确的数字。该对象具有30个属性,但count()函数的返回值为1。
有任何想法吗?
以下是其中一个对象的示例。(我正在从Twitter请求每日趋势信息)。如果此对象具有多个属性,则count($ obj)等于1。
[trends] => stdClass Object ( [2009-08-21 11:05] => Array ( [0] => stdClass Object ( [query] => "Follow Friday" [name] => Follow Friday ) [1] => stdClass Object ( [query] => "Inglourious Basterds" OR "Inglorious Basterds" [name] => Inglourious Basterds ) [2] => stdClass Object ( [query] => Inglourious [name] => Inglourious ) [3] => stdClass Object ( [query] => #songsincode [name] => #songsincode ) [4] => stdClass Object ( [query] => #shoutout [name] => #shoutout ) [5] => stdClass Object ( [query] => "District 9" [name] => District 9 ) [6] => stdClass Object ( [query] => #howmanypeople [name] => #howmanypeople ) [7] => stdClass Object ( [query] => Ashes OR #ashes [name] => Ashes ) [8] => stdClass Object ( [query] => #youtubefail [name] => #youtubefail ) [9] => stdClass Object ( [query] => TGIF [name] => TGIF ) [10] => stdClass Object ( [query] => #wish09 [name] => #wish09 ) [11] => stdClass Object ( [query] => #watch [name] => #watch ) [12] => stdClass Object ( [query] => Avatar [name] => Avatar ) [13] => stdClass Object ( [query] => Ramadhan [name] => Ramadhan ) [14] => stdClass Object ( [query] => Goodnight [name] => Goodnight ) [15] => stdClass Object ( [query] => iPhone [name] => iPhone ) [16] => stdClass Object ( [query] => #iranelection [name] => #iranelection ) [17] => stdClass Object ( [query] => Apple [name] => Apple ) [18] => stdClass Object ( [query] => "Usain Bolt" [name] => Usain Bolt ) [19] => stdClass Object ( [query] => H1N1 [name] => H1N1 ) ) )
答案1
小编典典问题在于count的目的是对数组中的索引进行计数,而不是对对象的属性进行计数(除非它是实现Countable接口的自定义对象)。尝试像下面那样将对象转换为数组,看看是否有帮助。
$total = count((array)$obj);
简单地将对象转换为数组并不总是可行,但是作为一个简单的stdClass对象,它应该可以在这里完成工作。
PHP / JSON – stdClass对象
stdClass Object ( [2010091907] => stdClass Object ( [home] => stdClass Object ( [score] => stdClass Object ( [1] => 7 [2] => 17 [3] => 10 [4] => 7 [5] => 0 [T] => 41 ) [abbr] => ATL [to] => 2 )
这实际上是继续的 – 但是 – 我的问题是stdClass对象部分.我需要能够在for循环中调用它,然后遍历每个部分(home,score,abbr,to等).我该怎么办?
例:
<?PHP $foo = array('123456' => array('bar' => array('foo'=>1,'bar'=>2))); //as object var_dump($opt1 = json_decode(json_encode($foo))); echo $opt1->{'123456'}->bar->foo; foreach(get_object_vars($opt1->{'123456'}->bar) as $key => $value){ echo $key.':'.$value.PHP_EOL; } //as array var_dump($opt2 = json_decode(json_encode($foo),true)); echo $opt2['123456']['bar']['foo']; foreach($opt2['123456']['bar'] as $key => $value){ echo $key.':'.$value.PHP_EOL; } ?>
输出:
object(stdClass)#1 (1) { ["123456"]=> object(stdClass)#2 (1) { ["bar"]=> object(stdClass)#3 (2) { ["foo"]=> int(1) ["bar"]=> int(2) } } } 1 foo:1 bar:2 array(1) { [123456]=> array(1) { ["bar"]=> array(2) { ["foo"]=> int(1) ["bar"]=> int(2) } } } 1 foo:1 bar:2
PHP / JSON-stdClass对象
我对数组还很陌生。我需要一些帮助-我有一些JSON,并且已经通过一些PHP对其进行了运行,这些PHP基本上可以解析JSON并对其进行解码,如下所示:
stdClass Object
(
[2010091907] => stdClass Object
(
[home] => stdClass Object
(
[score] => stdClass Object
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
实际上,这种情况一直持续发生-但是-我的问题是这一stdClass
Object
部分。我需要能够在for循环中调用此函数,然后遍历每个部分(主页,得分,缩写,到等)。我将如何处理?
PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass
PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误:
复制代码 代码如下:
Cannot use object of type stdClass as array
产生原因:
复制代码 代码如下:
$res = json_decode($res);
$res[''key'']; //把 json_decode() 后的对象当作数组使用。
解决方法(2种):
1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。
2、json_decode($res) 返回的是一个对象, 不可以使用 $res[''key''] 进行访问, 换成 $res->key 就可以了。
php : Uncaught SoapFault exception: [SOAP-ENV:Server] Cannot use object of type stdClass as array
确定你的mysql驱动放在项目中了么?
1)启动Tomcat服务器,打开浏览器,输入(其中localhost是名称服务器或称为主机),
进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,
2)选择Resources-Data sources进入配置数据源界面,选择
Data Source Actions ->选择Create New Data Source,进入配置详细信息界面
主要内容例如下:
JNDI Name: ->jdbc/mysql
Data Source URL ->jdbc:mysql://localhost:3306/test
JDBC Driver Class-> org.gjt.mm.mysql.Driver
3)修改\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如testpool.xml)打开添加内容如下:
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
内容同conf/server.xml中
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
少了这一步会报错:Cannot create JDBC driver of class '''' for connect URL......余下全文>>
怎处理Fatal error: Cannot use object of type stdClass as array in
json数据格式没解析出来
PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass_PHP教程
PHP JSON出错:Cannot use object of type stdClass as array解决方法,jsonstdclass
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误:
Cannot use object of type stdClass as array
产生原因:
$res = json_decode($res);
$res[''key'']; //把 json_decode() 后的对象当作数组使用。