Home  >  Article  >  Backend Development  >  PHP two-dimensional array sorting and traversal_PHP tutorial

PHP two-dimensional array sorting and traversal_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:40917browse

I won’t say much about the definition of two-dimensional arrays in PHP. It’s very simple. The following examples are all about PHP two-dimensional array sorting and traversal. Each of our examples introduces PHP two-dimensional array sorting in a different way. . ​

I won’t say much about the definition of two-dimensional arrays in the PHP tutorial. It’s very simple. The following examples are all about php two-dimensional array sorting and traversal. Each of our examples is Introducing PHP two-dimensional array sorting in different ways.

function multi_array_sort($multi_array,$sort_key,$sort=sort_asc){
If(is_array($multi_array)){
foreach ($multi_array as $row_array){
If(is_array($row_array)){
$key_array[] = $row_array[$sort_key];
                                                                                               Return -1;
                                                                                                                                                   }else{
Return -1;
}  
Array_multisort($key_array,$sort,$multi_array);
Return $multi_array;
}


array(
               array('id'=>1,'data'=>'a'),

               array('id'=>3,'data'=>'c'),
               array('id'=>2,'data'=>'b')
);

PHP itself has a function for sorting multi-dimensional arrays.

bool array_multisort ( array $ar1 [, mixed $arg [, mixed $... [, array $... ]]] )

Method 2


//Use the time inputdate as the key value to sort the two-dimensional array in descending order
function array_sort($arr,$keys,$type){

$keysvalue=array(); $i = 0;
foreach($arr as $key=>$val) {
$val[$keys] = str_replace("-","",$val[$keys]);
$val[$keys] = str_replace(" ","",$val[$keys]);
$val[$keys] = str_replace(":","",$val[$keys]);
$keysvalue[] =$val[$keys];

}
asort($keysvalue); //key value sorting

reset($keysvalue); //Redirect the pointer to the first

in the array foreach($keysvalue as $key=>$vals) {
$keysort[] = $key;
}
$new_array = array();
if($type != "asc"){
for($ii=count($keysort)-1; $ii>=0; $ii--) {
$new_array[] = $arr[$keysort[$ii]];
}
}else{
for($ii=0; $ii
$new_array[] = $arr[$keysort[$ii]];
}
} return $new_array;
}
?>


($keysort);>Method 3

    $a = array(
       0=>array("id"=>1,"name"=>"小何","addr"=>"0:1"),
       1=>array("id"=>2,"name"=>"小刚","addr"=>"0:1:2"),
       2=>array("id"=>3,"name"=>"小军","addr"=>"0:1:3"),
       3=>array("id"=>4,"name"=>"公司","addr"=>"0:4"),
    );
function sort_by($array, $keyname = null, $sortby){
    $myarray = $inarray = array();
    foreach($array as $i=>$befree){
      $myarray[$i] = $array[$i][$keyname];
    }
    switch($sortby){
       case 'asc':
       asort($myarray);
       break;
       case 'arsort':
       arsort($myarray);
       break;
       case 'natcasesor':
       natcasesor($myarray);
       break;
    }
    foreach($myarray as $key=>$befree){
        $inarray[$key] = $array[$key];
        }
          return $inarray;
}
print_r(sort_by($a,"addr","arsort"));


print_r($a[2]["id"]);
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445421.htmlTechArticle关于php二维数组定义我就不说多了,很简单的,下面几款实例都是讲关于php 二维数组排序与遍历哦,我们每个实例都是以不同的方法介绍...
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