php Array Traversal Classes and Usage Examples

  • 2021-12-11 17:30:55
  • OfStack

This article illustrates the php array traversal class and its usage. Share it for your reference, as follows:


<?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.'<br />';
        }
      }
      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;

Run results:

array[g]=a
array[vv][b]=b
array[vv][l]=c
array[vv][xx][0]=e
array[vv][xx][1]=g

For more readers interested in PHP related contents, please check the special topics of this site: "PHP Array (Array) Operation Skills Encyclopedia", "php Sorting Algorithm Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary" and "PHP Common Traversal Algorithms and Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: