zoukankan      html  css  js  c++  java
  • PHP数组函数

    <?php
    //1.计算数组中所有值的乘积array_product
    $a = array(1, 2, 3, 4);
    echo "product(a) = " . array_product($a);
    echo "<br><br>";

    //2.从数组中随机取出一个或多个单元array_rand
    $input = array("a", "b", "c", "d", "e");
    $rand_keys = array_rand($input, 2);
    echo $input[$rand_keys[0]];;
    echo "<br>";

    //3.将数组最后一个单元移除数组array_pop
    $stack = array("0", "1", "2", "3","4");
    $fruit = array_pop($stack);
    var_dump($stack);
    echo "<br>";


    //4.在数组开头插入一个或多个单元array_unshift
    $queue = array("1", "2");
    array_unshift($queue, "3", "4");
    var_dump($queue);
    echo "<br>";

    //5.将一个或多个单元压入数组的末尾(入栈)array_push
    $stack = array("1", "2");
    array_push($stack, "3", "4");
    var_dump($stack);
    echo "<br>";

    //6.计算数组中所有值的和array_sum
    $a = array(2, 4, 6, 8);
    echo "sum(a) = ".array_sum($a);
    echo "<br>";
    $b = array("a" => 1.1, "b" => 1.2, "c" => 1.3);
    echo "sum(b) = " . array_sum($b);
    echo "<br>";

    //7.移除数组中重复的值array_unique
    $input = array(4, 4, "3", 4, 3, "3",1,1,1,2,3,5,4,2,3);
    $result = array_unique($input);
    var_dump($result);
    echo "<br>";

    //8.对数组进行排序并保持索引关系asort
    $fruits = array( "b", "a", "z", "d");
    asort($fruits);
    var_dump($fruits);
    echo "<br>";

    //9.对数组进行逆向排序并保持索引关系arsort
    $fruits = array( "b", "a", "z", "d");
    arsort($fruits);
    var_dump($fruits);
    echo "<br>";

    //10.键名的正序对数组进行排序ksort
    $fruits = array( "b", "a", "z", "d");
    ksort($fruits);
    var_dump($fruits);
    $fruits = array( "b"=>"b", "a"=> "a", "z"=>"z", "d"=>"d");
    ksort($fruits);
    var_dump($fruits);
    echo "<br>";

    //11.键名的倒序对数组进行排序krsort
    $fruits = array( "b", "a", "z", "d");
    krsort($fruits);
    var_dump($fruits);
    $fruits = array( "b"=>"b", "a"=> "a", "z"=>"z", "d"=>"d");
    krsort($fruits);
    var_dump($fruits);
    echo "<br>";


    //12.键值的正序对数组进行排序sort
    $fruits = array("lemon", "orange", "banana", "apple");
    sort($fruits);
    var_dump($fruits);
    echo "<br>";


    //13.键值的倒序对数组进行排序rsort
    $fruits = array("lemon", "orange", "banana", "apple");
    rsort($fruits);
    var_dump($fruits);
    echo "<br>";

    //14.把数组中的值赋给一些变量list
    $a=array(18,20,25);
    list($zhangsan,$lisi,$wangwu)=$a;
    echo $zhangsan;
    echo"<br>";
    echo $lisi;
    echo"<br>";
    echo $wangwu;
    ?>

  • 相关阅读:
    Linux常用命令(文件常用命令)
    Spring中的核心思想
    AOP中重要概念
    Spring框架中2种生成代理对象的方法
    python模块详解 | psutil
    Linux学习笔记 | 常见错误之无法获得锁
    python学习笔记 | 列表去重
    CentOS | python3.7安装指南
    selenium爬虫 | 爬取疫情实时动态(二)
    selenium爬虫 | 爬取疫情实时动态
  • 原文地址:https://www.cnblogs.com/yanjialin/p/3691962.html
Copyright © 2011-2022 走看看