Home > php教程 > php手册 > 插入数组但不影响原来排序

插入数组但不影响原来排序

WBOY
Release: 2016-06-06 19:31:43
Original
1369 people have browsed it

插入数组但不影响原来排序-PHP 无 源码与演示: 源码出处演示出处 function array_intsort($array,$num) { $array_right = $array_left = array(); $length = count($array); if ($num $array[0]) { array_unshift($array,$num); return $array; } else { for

插入数组但不影响原来排序 - PHP

源码与演示:源码出处 演示出处

 function array_intsort($array,$num) {
    $array_right = $array_left = array();
      
    $length = count($array);
    if ($num < $array[0]) {
        array_unshift($array,$num);
        return $array;
    } else {
        for($i=0; $i < $length; $i++) {
            if ($i + 1 < $length) {
                if ($array[$i] < $num && $num < $array[$i+1]) {
                    $array_right = array_slice($array,$i + 1);           
                    $array_left = array_slice($array,0,$i);
                    $array_left[] = $num;               
                    break;
                }
            } else {
                $array_left = $array;
                $array_left[] = $num;           
            }
        }
        return array_merge($array_left,$array_right);
    }
}
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template