PHP array traversal class and usage example

Time:2019-10-9

This paper describes the traversal classes and usage of PHP arrays with examples. Share 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;

Operation results:

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

More readers interested in PHP-related content can see the topics of this site: PHP Array Operating Skills, Php Sorting Algorithms Summary, PHP Data Structure and Algorithms Tutorial, Php Programming Algorithms Summary, Php String Usage Summary, and PHP Traversal Algorithms and Techniques Summary.

I hope this article will be helpful to everyone’s PHP program design.