PHP:计数一个stdClass对象(PHP: Count a stdClass object)
我有一个从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 ) ) )
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.
Any ideas?
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 ) ) )
原文:https://stackoverflow.com/questions/1314745
最满意答案
他们有不同的目的。
- 如果要存储与元素关联的一般数据,请使用
.data()
。- 如果要更改DOM元素的属性,请使用
.attr()
。不要“发明”新的属性(从HTML5数据属性开始 )。 但是,当你使用jQuery时,坚持使用
.data()
。They serve different purposes.
- If you want to store general data associated with an element, use
.data()
.- If you want to change the attributes of the DOM element, use
.attr()
.Don't "invent" new attributes (appart from HTML5 data attributes). But as you are using jQuery, stick with
.data()
.
相关问答
更多-
你只需要在其上放置双引号: You just need to put double quotes around it:Array.apply(null, document.querySelectorAll("[data-selected='1']")). forEach(function (e) { console.log(e.getAttribute('data-id')); });我认为两者在行为层面上是相同的。 正如@satpal在评论中所说。 我试过jsPref,它说$('*[onclick]')是最快的。 谢谢@satpal I think Both are same in behavioral level. As @satpal said in comment. I tried in jsPref, It says $('*[onclick]') is the fastest. Thanks @satpal建议使用jQuery.data()可以读取的data-属性 得到 alert( $('#test').data('foo')) 组 $('#test').data('foo','new value, or object or array')) 不必存在使用此属性的属性。 您可以随时在元素上存储任何内容,也可以在标记中读取数据。 jQuery.data()API文档 Suggest using data- attributes t ...我不推荐它,但你可以这样做: lapply(myList, 'attr<-', which='myname', value='myStaticName') 。 旧的for循环可能是执行此任务的最清晰的方法 - 或者在创建对象时在上游执行此分配。 for (i in seq_along(myList)) attr(myList[[i]], 'myname') <- 'myStaticName' 编辑: 正如@mnel在注释中指出的那样, data.table包中的setattr也是一个有效的选项,因为它通过 ...我认为你对.data()的作用感到困惑: .data(name,value)将信息存储在内部jQuery缓存中 - 您无法在DOM结构中看到它。 .attr(name,value)更改该元素的DOM属性。 因此,如果您检查它,您将能够在HTML代码中看到它。 I think you are confused about what .data() does: .data(name,value) stores the information in an internal jQuery cache - you c ....attr将读取实际属性 。 .data将读取该数据项的当前值 。 如果你有代码改变了值(即$('#getMessage').data('sid',160) )那将是当前值。 .attr will read the actual attribute. .data will read the current value of that data item. If you have code which has changed the value (ie, $('#getMessage').data('sid' ...
相关文章
更多- Object Oriented Programming
- Guava学习笔记:复写的Object常用方法
- Becoming a data scientist
- hive2solr时count的一个bug
- Spring Data: a new perspective of data operations
- Create a Bootable MicroSD Card
- Drupal Forums instead of phpBB or vBulletin: A casestudy
- [转]So You Want To Be A Producer
- She’s Not Carrying A Handbag
- Solr PHP support
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)