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

Two ways to traverse multidimensional arrays in php

墨辰丷
Release: 2023-03-29 21:32:01
Original
5455 people have browsed it

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;
       }
     }
   }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template