PHP Tip: Getting the property count for an object
2007-06-02 00:16
543 查看
原文:http://www.ejeliot.com/blog/86
The other day I was working on an application which uses PHP's built in JSON libraries to parse data returned from the del.icio.us APIs. The JSON libraries return tag data for each post as an object rather than an array as shown below:
This means that you can't directly use the count() function to work out the number of tags associated with a particular post. The following won't work:
Luckily the solution is very straight forward - simply cast the object to an array before calling count:
The other day I was working on an application which uses PHP's built in JSON libraries to parse data returned from the del.icio.us APIs. The JSON libraries return tag data for each post as an object rather than an array as shown below:
stdClass Object(
[css] => 549
[webdesign] => 136
[web] => 75
[floats] => 113
[webdev] => 36
)
This means that you can't directly use the count() function to work out the number of tags associated with a particular post. The following won't work:
$iCount = count($oTags);
Luckily the solution is very straight forward - simply cast the object to an array before calling count:
$iCount = count((array)$oTags);
相关文章推荐
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- C# 编程遇到An object reference is required for the non-static field, method, or property错误
- Caught an exception while getting the property… Struts2报错
- custom format Property of business object for the data binding
- Incorrect decrement of the reference count of an object that is not owned at this point by the calle
- LINQ for SuperAdmin (Add the property count) -- join & count function
- Incorrect decrement of the reference count of an object that is not owned at this point by the caller1
- Incorrect decrement of the reference count of an object that is not owned at this point by the caller1
- [转] When exactly does the virtual table pointer (in C++) gets set for an object?
- struts2 <debug>标签报错:Caught an exception while getting the property values of null
- Incorrect decrement of the reference count of an object that is not owned at this point by the calle
- Android An unexpected exception occurred while creating a change object. see the error log for more details
- Question 30: What is the order of destructor calls for an object of class Y inherited from class X that has an object of class A
- XPathFactory#newInstance() failed to create an XPathFactory for the default object 报错解决办法整理
- Specified ID:supplyItemLocalDS collides with the ID for an existing SmartGWT component or object.
- If you object has an "Id' property, it will be set with the generated Id from MongoDB.
- In C++, which of the following syntax can be used for instantiating an object named arr of the vecto
- 'date' is an invalid value for the SoapElementAttribute.DataType property. The property may only be specified for primitive types.
- Noticed a strange thing the other day, a list has the property ItemCount and also an Items collection which has its own .Count b
- 奇怪的内存泄露 incorrect decrement of the reference count of an object that is not owned at this point by