zoukankan      html  css  js  c++  java
  • PHP基础-常用的数组相关处理函数

    一 数组键/值操作有关的函数
      1. array_values()//获取数组中所有的值

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySqL", "laguage"=>"php");
    
    $arr=array_values($lamp);
    
    list($os, $wb, $db, $lang)=$arr;
    
    echo $os."<br>";
    echo $wb."<br>";
    echo $db."<br>";
    echo $lang."<br>";
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

      2. array_keys()//返回数组中所有的键

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php");
    三种用法:
    $arr=array_keys($lamp);
    $arr=array_keys($lamp, "MySql");
    $arr=array_keys($lamp, "100", true);
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

      3. in_array() //判断值是否在数组中

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php", "html"=>"100", array("a", "b"));
    
    if(in_array(array("a", "b"), $lamp)){
        echo "exists";
    }else{
        echo "not exists";
    }
    
    if(in_array("100", $lamp, true)){
        echo "exists";
    }else{
        echo "not exists";
    }
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

      4. array_key_exists//判断数组中是否存在相应的键

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php", "html"=>"100", array("a", "b"));
    
    if(array_key_exists("os1", $lamp)){
        echo "exists";
    }else{
        echo "not exists";
    }
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

      5.array_flip -- 交换数组中的键和值

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php", "html"=>"100", 10=>"linux");
    
    $arr=array_flip($lamp);
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>';

      6. array_reverse -- 返回一个单元顺序相反的数组 

    $lamp=array("os"=>"linux", "webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php", "html"=>"100", 10=>"linux");
    
    $arr=array_reverse($lamp);
    
    echo '<pre>';
    print_r($arr);
    echo '</pre>'

    二、 统计数组元素的个数和惟一性

      1. count() sizeof();

      2. array_count_values -- 统计数组中所有的值出现的次数
      3. array_unique -- 移除数组中重复的值

    三、使用回调函数处理数组的函数

      1. array_filter() 用回调函数过滤数组中的单元
      2. array_walk() 数组中的每个成员应用用户函数

      3. array_map() 将回调函数作用到给定数组的单元上

    四、数组的排序函数
      sort()、rsort()
      usort()、asort()
      arsort()、uasort()
      ksort()、krsort()、uksort()、  

          natsort()、natcasesort()
      array_multisort()
      1. 简单的数组排序
         sort() rsort()
      2. 根据键名对数组排序
         ksort() krsort()
      3. 根据元素的值对数组排序
         asort() arsort()
      4. 根据“自然数排序”法对数组排序
         natsort() natcasesort()
      5. 根据用户自定义规则对数组排序
         usort() uasort() uksort()
      6.多维数组的排序
         array_multisort
    五、 拆分、合并、分解、接合的数组函数
      1. array_slice()
       2.array_splice()
      3. array_combine();
      4. array_merge();
      5. array_intersect();
      6. array_diff()

    六、 数组与数据结构的函数
       1. 使用数据实现堆栈
         array_push()
         array_pop()
      2. 使用队列
        array_unshift()
        array_shift()
        unset()
    七、 其它与数据操作有关的函数
      array_rand();
      shuffle()
      array_sum()
      range()

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/gpdm/p/6641648.html
Copyright © 2011-2022 走看看