search
Homephp教程php手册php 二维数组排序与遍历

php 二维数组排序与遍历

Jun 13, 2016 am 11:18 AM
phpunderandaboutseveral modelsdefinitionExampleIsortarrayofSimpleTraverse

关于php二维数组定义我就不说多了,很简单的,下面几款实例都是讲关于php 二维数组排序与遍历哦,我们每个实例都是以不同的方法介绍php 二维数组排序。  

关于php教程二维数组定义我就不说多了,很简单的,下面几款实例都是讲关于php 二维数组排序与遍历哦,我们每个实例都是以不同的方法介绍php 二维数组排序

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];  
            }else{  
                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本身是有一个多维数组排序的函数的。

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


方法二

 

//以时间inputdate为key值进行二维数组降序排列
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值排序
reset($keysvalue); //指针重新指向数组第一个
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);>

方法三

    $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"]);
?>


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft