PHP递归打印数组中所有元素

函数定义: 

//@patten:为打印前的字符串,不同的层次会增加该串打印的次数 
//@array:为要打印的数组 
function print_array($patten, $array){ 
  $retstr; 
  foreach($array as $value){ 
    if( is_array($value) ){ 
      $patten = $patten.$patten; 
      print_array($patten, $value); 
    } else { 
      echo "<p>".$patten."[".key($array)."]".": ".$value." <br/> ". "</p>"; 
    } 
  next($array); 
  } 
} 

调用方法:

print_array("-", $array); 

输出,打印sina微博:


-[created_at]: Sun Mar 25 16:35:50 +0800 2012
-[id]: 3427505904478654
-[mid]: 3427505904478654
-[idstr]: 3427505904478654
-[text]: 转发。
-[source]: iPhone客户端
-[favorited]:
-[truncated]:
-[in_reply_to_status_id]:
-[in_reply_to_user_id]:
-[in_reply_to_screen_name]:
-[geo]:
-[user]:
--[id]: 1721825977
--[idstr]: 1721825977
--[screen_name]: 许小年
--[name]: 许小年
--[province]: 11
--[city]: 1000
--[location]: 北京
--[description]:
--[url]: http://blog.sina.com.cn/xuxiaonian
--[profile_image_url]: http://tp2.sinaimg.cn/1721825977/50/1270102276/1
--[profile_url]: xuxiaonian
--[domain]: xuxiaonian
--[weihao]:
--[gender]: m
--[followers_count]: 3220389
--[friends_count]: 102
--[statuses_count]: 2249
--[favourites_count]: 489
--[created_at]: Thu Apr 01 00:00:00 +0800 2010
--[following]: 1
--[allow_all_act_msg]:
--[geo_enabled]: 1
--[verified]: 1
--[verified_type]: 0
--[remark]:
--[allow_all_comment]: 1
--[avatar_large]: http://tp2.sinaimg.cn/1721825977/180/1270102276/1
--[verified_reason]: 中欧国际工商学院经济学与金融学教授 许小年
--[follow_me]:
--[online_status]: 1
--[bi_followers_count]: 85
--[lang]: zh-cn
--[retweeted_status]:
----[created_at]: Sun Mar 25 15:14:44 +0800 2012
----[id]: 3427485494637159
----[mid]: 3427485494637159
----[idstr]: 3427485494637159
----[text]: 【观点·吴敬琏:中经济矛盾社会矛盾几乎到临界点】目前国家权力对市场的干预,对价格管控,造成很大权力寻租空间,腐败蔓延不能有效制止。这也同时带来经济问题社会问题变成了政治问题。1992年已明确的顶层设计是市场经济改革,可如今人们数典忘祖,忘了这个改革目标——今日,吴敬琏在岭南论坛表示。
----[source]: 新浪微博
----[favorited]:
----[truncated]:
----[in_reply_to_status_id]:
----[in_reply_to_user_id]:
----[in_reply_to_screen_name]:
----[thumbnail_pic]: http://ww3.sinaimg.cn/thumbnail/61e04755jw1drc2dpro6fj.jpg
----[bmiddle_pic]: http://ww3.sinaimg.cn/bmiddle/61e04755jw1drc2dpro6fj.jpg
----[original_pic]: http://ww3.sinaimg.cn/large/61e04755jw1drc2dpro6fj.jpg
----[geo]:
----[user]:
--------[id]: 1642088277
--------[idstr]: 1642088277
--------[screen_name]: 财经网
--------[name]: 财经网
--------[province]: 11
--------[city]: 5
--------[location]: 北京 朝阳区
--------[description]: 价值观决定影响力——《财经网》; 与您共同 “微观”这个时代的方方面面——《财经网微博》。
--------[url]: http://www.caijing.com.cn/
--------[profile_image_url]: http://tp2.sinaimg.cn/1642088277/50/1283204216/1
--------[profile_url]: caijing
--------[domain]: caijing
--------[weihao]:
--------[gender]: m
--------[followers_count]: 2069308
--------[friends_count]: 1405
--------[statuses_count]: 35761
--------[favourites_count]: 48
--------[created_at]: Fri Aug 28 00:00:00 +0800 2009
--------[following]:
--------[allow_all_act_msg]:
--------[geo_enabled]: 1
--------[verified]: 1
--------[verified_type]: 3
--------[remark]:
--------[allow_all_comment]: 1
--------[avatar_large]: http://tp2.sinaimg.cn/1642088277/180/1283204216/1
--------[verified_reason]: 财经网官方微博
--------[follow_me]:
--------[online_status]: 0
--------[bi_followers_count]: 835
--------[lang]: zh-cn
--------[reposts_count]: 2721
--------[comments_count]: 562
--------[mlevel]: 0
--------[visible]:
----------------[type]: 0
----------------[list_id]: 0
----[reposts_count]: 756
----[comments_count]: 190
----[mlevel]: 0
----[visible]:
--------[type]: 0
--------[list_id]: 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值