php数组遍历类与用法示例

这篇文章主要介绍了php数组遍历类与用法,结合实例形式分析了php基于面向对象的数组遍历、读取操作封装与使用技巧,需要的朋友可以参考下。

本文实例讲述了php数组遍历类与用法,分享给大家供大家参考,具体如下:

  1. <?php
  2. class scanArray{
  3. public $arr;
  4. public $where;
  5. private $str;
  6. public function scan($arr,$where="array"){
  7. $this->arr = $arr;
  8. $this->where = $where;
  9. foreach($this->arr as $k=>$v){
  10. if(is_array($v)){
  11. $this->where = ($this->where)."[{$k}]";
  12. $this->scan($v,$this->where);
  13. }else{
  14. $this->str .= $this->where."[{$k}]=".$v.'<br />';
  15. }
  16. }
  17. return $this->str;
  18. }
  19. function __destruct(){
  20. unset($this->arr);
  21. unset($this->where);
  22. }
  23. }
  24. $a = array('g'=>"a",'vv'=>array("b"=>"b","l"=>"c","xx"=>array("e","g")));
  25. $ah = new scanArray();
  26. $b = $ah->scan($a);
  27. echo $b;

运行结果:

  1. array[g]=a
  2. array[vv][b]=b
  3. array[vv][l]=c
  4. array[vv][xx][0]=e
  5. array[vv][xx][1]=g