美文网首页
php常见数组函数

php常见数组函数

作者: 西兰花伟大炮 | 来源:发表于2017-06-15 20:04 被阅读24次
    (1)array_rand(array,number) //number指定返回随机( 键名 )的多少
    <?php
    $a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    print_r(array_rand($a,1));
    ?>
    

    返回数为1,将返回一个随机键名

    <?php
    $a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    print_r(array_rand($a,2));    //Array ( [0] => a [1] => d ) 
    ?>
    

    返回数多个,将返回随机带键名的数组,经常与array_flip()配合使用


    (2)range(from,to,length);

    print_r(range(0,20,2));
    第三个参数是数组元素之间的步长


    (3)数组遍历

    foreach有两种遍历方式,带键名与不带键名,只能遍历数组与对象

    $list = array('weidapao',"jack","ugly",'breast','army','joe');
    
    for($i = 0;$i < count($list);$i++){
        echo $list[$i]."<br>";
    };
    
    foreach ($list as $key => $value) {
        echo $value."<br>";
    }
    echo "<br>";
    
    $hotStone = [
        '战士' => '奥金斧',
        '猎人'=>'恐龙学',
        '法师'=>'法力浮龙',
        '牧师'=>'龙息药水',
        '萨满'=>'蛮鱼图腾'
    ];
    foreach($hotStone as $key=>$val){
        echo '职业是:'.$key.'-特点是:'.$val.'<br>';
    };
    
    while($a = each($list)){
        echo $a['key']."-".$a['value'].'<br>';
    }
    
    (4)list解构赋值
    $hero = array("weien","blind","crocodile");
    
    list($adc,$jungle,$top) = $hero;
    
    echo "abc 是 $adc | jungle 是 $jungle | top 是 $top";
    
    (5)不改变原数组的一些数组方法
    $multi = array(5,8,9,7,6,6,5,5);
    $unique = array_unique($multi);
    print_r($unique); //不会改变原数组
    

    array_flip(array); 交换数组的键名与键值

    $relative = array('name'=>'weiwei','age'=>22,'salary'=>2200);
    $turnover = array_flip($relative);
    print_r($turnover);   //交换数组的键名与键值
    
    (6)排序函数

    ksort() 函数对关联数组按照键名进行升序排序
    请使用asort() 函数对关联数组按照键值进行升序排序

    (7)数组插入方式
    $table = array();
    
    $table[] = 'watar';
    $table[] = 'rice';
    $table[] = 'salt';
    print_r($table);  //Array ( [0] => watar [1] => rice [2] => salt ) 
    
    (8)遍历二维数组
    $user = [
        ["id"=>1,"name"=>'fighter',"salary"=>'5000'],
        ["id"=>2,"name"=>'hank',"salary"=>'6000'],
        ["id"=>3,"name"=>'brumu',"salary"=>'4000'],
        ["id"=>4,"name"=>'coco',"salary"=>'3000'],
        ["id"=>5,"name"=>'gorge',"salary"=>'2000'],
        ["id"=>6,"name"=>'lesbin',"salary"=>'1000']
    ];
    foreach($user as $val){
        echo '编号是:'.$val['id'];
        echo '名字是:'.$val['name'];
        echo '工资是:'.$val['salary'];
        echo "<br>";
    };
    
    (9)去掉字符串指定分隔,并将分隔后第一个字母转大写
    $test = "wei_da_pao";
    
    function transfer($str){
        $return = '';
        $str = explode('_',$str);
    
        foreach ($str as $value) {
            $return .= ucfirst($value);
        };
        return $return;
    }
    //Notice: Undefined variable: return in D:\phpDemo\all\php_Basic\str_func.php on line 
    var_dump(transfer($test));
    

    使用拼接字符串变量前需要先声明为字符串,不然会产生Notice警告

    echo lcfirst('bady');
    echo "<br>";
    echo ucwords('we are the best man, you are good enough');
    echo "<br>";
    echo strtolower('YAKEXI');
    echo "<br>";
    echo strtoupper('leikesa');
    
    image.png
    • lcfirst() 把字符串中的首字符转换为小写
    • ucwords() 把字符串中每个单词的首字符转换为大写
    • strtoupper() 把字符串转换为大写
    • strtolower() 把字符串转换为小写

    相关文章

      网友评论

          本文标题:php常见数组函数

          本文链接:https://www.haomeiwen.com/subject/pejwwttx.html