PHP Tip: Getting the property count for an object

本文介绍了一种在使用PHP内置JSON库解析del.icio.us API返回的数据时遇到的问题及解决方案。由于JSON库将标签数据作为对象而非数组返回,直接使用count()函数无法计算标签数量。文章提供了一个简单的方法,通过将对象转换为数组来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文: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:

  1. stdClass Object(
  2. [css] => 549
  3. [webdesign] => 136
  4. [web] => 75
  5. [floats] => 113
  6. [webdev] => 36
  7. )

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:

  1. $iCount = count($oTags);

Luckily the solution is very straight forward - simply cast the object to an array before calling count:

  1. $iCount = count((array)$oTags); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值