Home > Backend Development > PHP Tutorial > How to implement array sorting by specified KEY in PHP_PHP Tutorial

How to implement array sorting by specified KEY in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 09:58:57
Original
1064 people have browsed it

How to sort arrays by specified KEY in php

This article mainly introduces the method of sorting arrays in php by specified KEY. It involves the skills of sorting arrays in php. It has certain For reference value, friends in need can refer to it

The example in this article describes how PHP implements array sorting by specified KEY. Share it with everyone for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function array_sort($arr,$keys,$orderby='asc'){

$keysvalue = $new_array = array();

foreach ($arr as $k=>$v){

$keysvalue[$k] = $v[$keys];

}

if($orderby== 'asc'){

asort($keysvalue);

}else{

arsort($keysvalue);

}

reset($keysvalue);

foreach ($keysvalue as $k=>$v){

$new_array[] = $arr[$k];

}

return $new_array;

}

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16
function array_sort($arr,$keys,$orderby='asc'){ $keysvalue = $new_array = array(); foreach ($arr as $k=>$v){ $keysvalue[$k] = $v[$keys]; } if($orderby== 'asc'){ asort($keysvalue); }else{ arsort($keysvalue); } reset($keysvalue); foreach ($keysvalue as $k=>$v){ $new_array[] = $arr[$k]; } return $new_array; }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/976547.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976547.htmlTechArticleHow to sort arrays by specified KEY in php. This article mainly introduces how to sort arrays by specified KEY in php. , involving the skills of php operating array sorting, which has certain reference value...
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