PHP数组遍历类功能概述
本内容详细解析了PHP中用于数组遍历的类设计思路与实际应用方式,旨在为开发者提供实用的参考代码。
<?php
class scanArray{
public $arr;
public $where;
private $str;
public function scan($arr,$where="array"){
$this->arr = $arr;
$this->where = $where;
foreach($this->arr as $k=>$v){
if(is_array($v)){
$this->where = ($this->where)."[{$k}]";
$this->scan($v,$this->where);
}else{
$this->str .= $this->where."[{$k}]=".$v.'
';
}
}
return $this->str;
}
function __destruct(){
unset($this->arr);
unset($this->where);
}
}
$a = array('g'=>"a",'vv'=>array("b"=>"b","l"=>"c","xx"=>array("e","g")));
$ah = new scanArray();
$b = $ah->scan($a);
echo $b;
以下是该类的实际执行效果演示:
array[g]=a array[vv][b]=b array[vv][l]=c array[vv][xx][0]=e array[vv][xx][1]=g
上述内容期望能为从事PHP程序开发的读者带来实质性的帮助与启发。