Home > Backend Development > PHP Tutorial > 用户自定义排序数组有关问题

用户自定义排序数组有关问题

WBOY
Release: 2016-06-13 10:09:04
Original
887 people have browsed it

用户自定义排序数组问题

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?PHP $number = array(array('tir','tire','100'),                array('Oil','oil','50'),                array('spk','spark','10'));                function compare($x,$y){                if($x[1]==$y[1]){                                return 0;}                else if($x[1]<$y[1]){                return -1;                }                else{                                return 1;}                }usort($number,'compare');print_r($number);?>        
Copy after login


那个function函数是什么意思 看不懂 哪些返回值是怎么回事 原理究竟是什么 请高手详细讲解一下 小弟刚学PHP

------解决方案--------------------
我的理解,lz可以参考下我下面的例子,希望对你有所帮助
PHP code
$number = array(        array('tir1','tire2','100'),                array('Oil1','oil2','50'),                array('spk1','spark2','10')                     );                    function compare($x,$y){    if($x[1]==$y[1]){         return 0;     } else if($x[1]';usort($number,'compare');print_r($number);// 函数compare 是比较函数 ,  也就是把$number数组值代入compare这个函数中进行比较,根据比较的结果,重新排序这个$number数组的内部值//首先比较的是$x[1]==$y[1]//也就是oil2 跟 tire2 进行比较 ,显然他们的值不一样,于是进行$x[1]
                 
              
              
        
            
Copy after login
Related labels:
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