<tfoot id='9Jc5M'></tfoot>
    1. <legend id='9Jc5M'><style id='9Jc5M'><dir id='9Jc5M'><q id='9Jc5M'></q></dir></style></legend>

    2. <small id='9Jc5M'></small><noframes id='9Jc5M'>

          <bdo id='9Jc5M'></bdo><ul id='9Jc5M'></ul>
      1. <i id='9Jc5M'><tr id='9Jc5M'><dt id='9Jc5M'><q id='9Jc5M'><span id='9Jc5M'><b id='9Jc5M'><form id='9Jc5M'><ins id='9Jc5M'></ins><ul id='9Jc5M'></ul><sub id='9Jc5M'></sub></form><legend id='9Jc5M'></legend><bdo id='9Jc5M'><pre id='9Jc5M'><center id='9Jc5M'></center></pre></bdo></b><th id='9Jc5M'></th></span></q></dt></tr></i><div id='9Jc5M'><tfoot id='9Jc5M'></tfoot><dl id='9Jc5M'><fieldset id='9Jc5M'></fieldset></dl></div>
      2. PHP:计算一个 stdClass 对象

        时间:2023-07-15

          • <bdo id='RA2la'></bdo><ul id='RA2la'></ul>
              • <i id='RA2la'><tr id='RA2la'><dt id='RA2la'><q id='RA2la'><span id='RA2la'><b id='RA2la'><form id='RA2la'><ins id='RA2la'></ins><ul id='RA2la'></ul><sub id='RA2la'></sub></form><legend id='RA2la'></legend><bdo id='RA2la'><pre id='RA2la'><center id='RA2la'></center></pre></bdo></b><th id='RA2la'></th></span></q></dt></tr></i><div id='RA2la'><tfoot id='RA2la'></tfoot><dl id='RA2la'><fieldset id='RA2la'></fieldset></dl></div>

                  <legend id='RA2la'><style id='RA2la'><dir id='RA2la'><q id='RA2la'></q></dir></style></legend>
                    <tbody id='RA2la'></tbody>

                  <small id='RA2la'></small><noframes id='RA2la'>

                • <tfoot id='RA2la'></tfoot>
                • 本文介绍了PHP:计算一个 stdClass 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个从 json_decode 创建的 stdClass 对象,当我运行 count($obj) 函数时它不会返回正确的数字.该对象有 30 个属性,但 count() 函数的返回值是 1.

                  I have a stdClass object created from json_decode that won't return the right number when I run the count($obj) function. The object has 30 properties, but the return on the count() function is say 1.

                  有什么想法吗?

                  以下是其中一个对象的示例.(我从 Twitter 请求每日趋势信息).如果此对象具有多个属性,则 count($obj) 将等于 1.

                  Below is an example of one of the objects. (I'm requesting the daily trend information from Twitter). If this object had more than one property, the count($obj) would equal 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
                                      )
                  
                              )
                       )
                  

                  推荐答案

                  问题是 count 的目的是计算数组中的索引,而不是对象上的属性,(除非它是实现了 Countable 接口的自定义对象).尝试将对象(如下所示)转换为数组,看看是否有帮助.

                  The problem is that count is intended to count the indexes in an array, not the properties on an object, (unless it's a custom object that implements the Countable interface). Try casting the object, like below, as an array and seeing if that helps.

                  $total = count((array)$obj);
                  

                  简单地将对象转换为数组并不总是有效,但作为一个简单的 stdClass 对象,它应该在这里完成工作.

                  Simply casting an object as an array won't always work but being a simple stdClass object it should get the job done here.

                  这篇关于PHP:计算一个 stdClass 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:当我们应该将构造函数设为 Private &amp;为什么?PHP 下一篇:mysql 计入 PHP 变量

                  相关文章

                  1. <i id='V2VgW'><tr id='V2VgW'><dt id='V2VgW'><q id='V2VgW'><span id='V2VgW'><b id='V2VgW'><form id='V2VgW'><ins id='V2VgW'></ins><ul id='V2VgW'></ul><sub id='V2VgW'></sub></form><legend id='V2VgW'></legend><bdo id='V2VgW'><pre id='V2VgW'><center id='V2VgW'></center></pre></bdo></b><th id='V2VgW'></th></span></q></dt></tr></i><div id='V2VgW'><tfoot id='V2VgW'></tfoot><dl id='V2VgW'><fieldset id='V2VgW'></fieldset></dl></div>
                  2. <small id='V2VgW'></small><noframes id='V2VgW'>

                    <legend id='V2VgW'><style id='V2VgW'><dir id='V2VgW'><q id='V2VgW'></q></dir></style></legend>
                    <tfoot id='V2VgW'></tfoot>

                      • <bdo id='V2VgW'></bdo><ul id='V2VgW'></ul>