Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (25) - array operation related functions (3) disassembly and arrangement - PhpStorm

Android programmers learn PHP development (25) - array operation related functions (3) disassembly and arrangement - PhpStorm

黄舟
黄舟Original
2017-03-03 09:50:461081browse

These three blog posts demonstrate almost all commonly used array functions. In PHP development, most of the time is spent on operating strings and arrays, so related functions are more important.

http://php.net/manual/zh/ref.array.php

Bubble sort
array_slice() Remove a segment from the array
array_splice() Remove part of the array and replace it with other values ​​
array_combine() Create an array, using the value of one array as its key name and the value of another array as its value

Array Addition and array merging
array_merge() Merge one or more arrays

Intersection and difference
array_intersect() Calculate the intersection of arrays
array_diff() Calculate the difference of arrays

Push and pop
array_push() Push one or more units to the end of the array (push)
array_pop() Pop the last unit of the array (pop)

Queue
array_unshift() Insert one or more cells at the beginning of the array (enqueue)
array_shift() Move the cells at the beginning of the array out of the array

Randomly
array_rand() From the array Randomly take out one or more units (randomly)

Shuffle
shuffle() Shuffle the array

Sum
array_sum() Calculate the sum of all values ​​in the array (Sum)

range() Create an array containing cells in the specified range
array_fill() Fill the array with the given value

<?php
    /**
     * 冒泡排序
     *
     * array_slice() 从数组中取出一段
     * array_splice() 把数组中的一部分去掉并用其它值取代
     * array_combine() 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
     *
     * 数组相加 与 数组合并
     * array_merge() 合并一个或多个数组
     *
     * 交集 与 差集
     * array_intersect() 计算数组的交集
     * array_diff() 计算数组的差集
     *
     * 入栈 与 出栈
     * array_push() 将一个或多个单元压入数组的末尾(入栈)
     * array_pop() 将数组最后一个单元弹出(出栈)
     *
     * 队列
     * array_unshift() 在数组开头插入一个或多个单元(入队)
     * array_shift() 将数组开头的单元移出数组
     *
     * 随机
     * array_rand() 从数组中随机取出一个或多个单元(随机)
     *
     * 打乱
     * shuffle() 将数组打乱
     *
     * 求和
     * array_sum() 计算数组中所有值的和(求和)
     *
     * range()  建立一个包含指定范围单元的数组
     * array_fill()  用给定的值填充数组
     */

    /**
     * 冒泡排序
     */
    echo &#39;---------- 冒泡排序 从小到大 ----------<br>&#39;;
    $arr = array(9,8,7,6,5,4,3,2,1,0);
    $tmp;
    for($i=0;$i<count($arr)-1;$i++ ){
        for($j=0;$j<count($arr)-1-$i;$j++){
            if($arr[$j] > $arr[$j+1]){
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $tmp;
            }
        }
    }
    print_r($arr); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
    echo &#39;<br>&#39;;

    echo &#39;---------- 冒泡排序 从大到小 ----------<br>&#39;;
    $arr = array(0,1,2,3,4,5,6,7,8,9);
    $tmp;
    for($i=0;$i<count($arr)-1;$i++ ){
        for($j=0;$j<count($arr)-1-$i;$j++){
            if($arr[$j+1] > $arr[$j]){
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $tmp;
            }
        }
    }
    print_r($arr); // Array ( [0] => 9 [1] => 8 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
    echo &#39;<br>&#39;;

    /**
     * array_slice() 从数组中取出一段
     */
    echo &#39;---------- array_slice() ----------<br>&#39;;
    $arr2 = array("a", "b", "c", "d", "e");
    $arr2n = array_slice($arr2, 2, 2); // 从下标2开始,取出2个元素
    print_r($arr2n); // Array ( [0] => c [1] => d )
    echo &#39;<br>&#39;;

    $arr2n2 = array_slice($arr2, -2, 2); // 从(最大下标-2)开始,取出2个元素
    print_r($arr2n2); // Array ( [0] => d [1] => e )
    echo &#39;<br>&#39;;

    /**
     * array_splice() 把数组中的一部分去掉并用其它值取代
     */
    echo &#39;---------- array_splice() ----------<br>&#39;;
    $arr2 = array("a", "b", "c", "d", "e");
    array_splice($arr2, 2); // 从下标2开始,去掉后面所有元素
    print_r($arr2); // Array ( [0] => a [1] => b )
    echo &#39;<br>&#39;;

    $arr2 = array("a", "b", "c", "d", "e");
    array_splice($arr2, -3, 2); // 从(最大下标-3)开始,去掉2个元素
    print_r($arr2); // Array ( [0] => a [1] => b [2] => e )
    echo &#39;<br>&#39;;

    $arr2 = array("a", "b", "c", "d", "e");
    array_splice($arr2, -3, 2, "hello"); // 从(最大下标-3)开始,去掉2个元素。从去掉的下标开始添加新元素
    print_r($arr2); // Array ( [0] => a [1] => b [2] => hello [3] => e )
    echo &#39;<br>&#39;;

    $arr2 = array("a", "b", "c", "d", "e");
    array_splice($arr2, -3, 2, array("hello", "world", "android")); // 从(最大下标-3)开始,去掉2个元素。从去掉的下标开始添加新元素
    print_r($arr2); // Array ( [0] => a [1] => b [2] => hello [3] => world [4] => android [5] => e )
    echo &#39;<br>&#39;;

    /**
     * array_combine() 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
     */
    echo &#39;---------- array_combine() ----------<br>&#39;;
    $arrA = array("os", "webserver", "db", "language");
    $arrB = array("linux", "apache", "mysql", "php");
    $arrC = array_combine($arrA,$arrB);
    print_r($arrC); // Array ( [os] => linux [webserver] => apache [db] => mysql [language] => php )
    echo &#39;<br>&#39;;

    /**
     * 数组相加
     * 相加时,下标相同时,$arrA的元素会覆盖$arrB的元素
     */
    echo &#39;---------- 数组相加 ----------<br>&#39;;
    $arrA = array("os", "webserver", "db", "language");
    $arrB = array("linux", "apache", "mysql", "php");
    $arrC = $arrA + $arrB;
    print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language )
    echo &#39;<br>&#39;;

    $arrA = array("os", "webserver", "db", "language");
    $arrB = array(9=>"linux", "apache", "mysql", "php");
    $arrC = $arrA + $arrB;
    print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language [9] => linux [10] => apache [11] => mysql [12] => php )
    echo &#39;<br>&#39;;

    /**
     * 数组合并
     * array_merge() 合并一个或多个数组
     * 如果下标是字符串,也就是说关联数组,下标相同时,$arrB的元素会覆盖$arrA的元素
     */
    echo &#39;---------- array_merge() 数组合并 ----------<br>&#39;;
    $arrA = array("os", "webserver", "db", "language");
    $arrB = array("linux", "apache", "mysql", "php");
    $arrC = array_merge($arrA, $arrB);
    print_r($arrC); // Array ( [0] => os [1] => webserver [2] => db [3] => language [4] => linux [5] => apache [6] => mysql [7] => php )
    echo &#39;<br>&#39;;

    $arrA = array("os", "two"=>"webserver", "db", "language");
    $arrB = array("linux", "two"=>"apache", "mysql", "php");
    $arrC = array_merge($arrA, $arrB);
    print_r($arrC); // Array ( [0] => os [two] => apache [1] => db [2] => language [3] => linux [4] => mysql [5] => php )
    echo &#39;<br>&#39;;

    /**
     * array_intersect() 计算数组的交集
     */
    echo &#39;---------- array_intersect() 交集 ----------<br>&#39;;
    $arrA = array("os", "www", "db", "linux");
    $arrB = array("linux", "apache", "mysql", "www");
    $arrC = array_intersect($arrA, $arrB);
    print_r($arrC); // Array ( [1] => www [3] => linux )
    echo &#39;<br>&#39;;

    /**
     * array_diff() 计算数组的差集
     */
    echo &#39;---------- array_diff() 差集 ----------<br>&#39;;
    $arrA = array("os", "www", "db", "linux");
    $arrB = array("linux", "apache", "mysql", "www");
    $arrC = array_diff($arrA, $arrB);
    print_r($arrC); // Array ( [0] => os [2] => db )
    echo &#39;<br>&#39;;

    /**
     * array_push() 将一个或多个单元压入数组的末尾(入栈)
     */
    echo &#39;---------- array_push() 入栈 ----------<br>&#39;;
    $arrZhan = array();
    array_push($arrZhan,"one");
    print_r($arrZhan); // Array ( [0] => one )
    echo &#39;<br>&#39;;
    array_push($arrZhan,"two");
    array_push($arrZhan,"three");
    print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three )
    echo &#39;<br>&#39;;
    array_push($arrZhan,"four","five");
    print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three [3] => four [4] => five )
    echo &#39;<br>&#39;;

    /**
     * array_pop() 将数组最后一个单元弹出(出栈)
     */
    echo &#39;---------- array_pop() 出栈 ----------<br>&#39;;
    array_pop($arrZhan);
    print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three [3] => four )
    echo &#39;<br>&#39;;

    echo array_pop($arrZhan); // four
    echo &#39;<br>&#39;;
    print_r($arrZhan); // Array ( [0] => one [1] => two [2] => three )
    echo &#39;<br>&#39;;

    /**
     * array_unshift() 在数组开头插入一个或多个单元(入队)
     */
    echo &#39;---------- array_unshift() 入队 ----------<br>&#39;;
    $arrDuilie = array();
    array_unshift($arrDuilie, "one");
    array_unshift($arrDuilie, "two");
    array_unshift($arrDuilie, "three");
    array_unshift($arrDuilie, "four");
    print_r($arrDuilie);
    echo &#39;<br>&#39;; // Array ( [0] => four [1] => three [2] => two [3] => one )

    /**
     * array_shift() 将数组开头的单元移出数组(出队)
     */
    echo &#39;---------- array_shift() 出队 ----------<br>&#39;;
    array_shift($arrDuilie);
    print_r($arrDuilie);
    echo &#39;<br>&#39;; // Array ( [0] => three [1] => two [2] => one )

    echo array_shift($arrDuilie); // three
    echo &#39;<br>&#39;;
    print_r($arrDuilie); // Array ( [0] => two [1] => one )
    echo &#39;<br>&#39;;

    /**
     * array_rand() 从数组中随机取出一个或多个单元(随机)
     */
    echo &#39;---------- array_rand() 随机 ----------<br>&#39;;
    $arrZ = array("a", "b", "c", "d", "e");
    var_dump(array_rand($arrZ)); // int(1)
    echo &#39;<br>&#39;;
    var_dump(array_rand($arrZ,3)); // array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3) }
    echo &#39;<br>&#39;;

    $randValue = array_rand($arrZ);
    $result = is_string($randValue); // 判断$randValue是不是字符串
    var_dump($result); // bool(false)
    echo &#39;<br>&#39;;

    /**
     * shuffle() 将数组打乱
     */
    echo &#39;---------- shuffle() 打乱 ----------<br>&#39;;
    $arrZ = array("a", "b", "c", "d", "e");
    shuffle($arrZ);
    print_r($arrZ); //Array ( [0] => c [1] => b [2] => d [3] => e [4] => a )
    echo &#39;<br>&#39;;

    /**
     * array_sum() 计算数组中所有值的和(求和)
     */
    echo &#39;---------- array_sum() 求和 ----------<br>&#39;;
    $arrH = array(1,2,3,4,5,6,7,8,9);
    echo array_sum($arrH); // 45
    echo &#39;<br>&#39;;

    /**
     * range()  建立一个包含指定范围单元的数组
     */
    echo &#39;---------- range() 求和 ----------<br>&#39;;
    $arrR = range(0,10);
    print_r($arrR); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 )
    echo &#39;<br>&#39;;

    $arrR = range(0,10,3); // 第三个参数表示步进(跳步)
    print_r($arrR); // Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 )
    echo &#39;<br>&#39;;

    /**
     * array_fill()  用给定的值填充数组
     */
    echo &#39;---------- array_fill() 求和 ----------<br>&#39;;
    $arrF = array_fill(0,5,"iwanghang");
    print_r($arrF); // Array ( [0] => iwanghang [1] => iwanghang [2] => iwanghang [3] => iwanghang [4] => iwanghang )
    echo &#39;<br>&#39;;

The above is how Android programmers learn PHP development ( 25)-Array operation related functions (3) Disassembly and arrangement-the content of PhpStorm, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


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