Home >Backend Development >PHP Tutorial >Two ways to traverse multidimensional arrays in php

Two ways to traverse multidimensional arrays in php

墨辰丷
墨辰丷Original
2018-06-05 17:44:565457browse

This article mainly introduces two methods of traversing multi-dimensional arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:

$a=array('fruits'=>array('a'=>'orange','b'=>'grape',c=>'apple'),
 'numbers'=>array(1,2,3,4,5,6),
 'holes'=>array('first',5=>'second','third')
 );
//第一种:
foreach($a as $list=>$things){
 if(is_array($things)){
 foreach($things as $newlist=>$counter){
 echo "key:".$newlist."<br/>"."value:".$counter."<br/>";
 }
}
}
//第二种:
function MulitarraytoSingle($array){
   $temp=array();
   if(is_array($array)){
     foreach ($array as $key=>$value )
     {
       if(is_array($value)){
         MulitarraytoSingle($value);
       }
       else{
         $temp[]=$value;
       }
     }
   }
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Analysis of examples of floating-point operation in PHP

##PHP implementation solution to obtain the real IP of the user client

php implements the method of obtaining the number of lines in a file

The above is the detailed content of Two ways to traverse multidimensional arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn