Home > Article > Backend Development > Two ways to traverse multidimensional arrays in php
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!