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

    1、strlen()函数返回字符串长度

    <?php
    echo strlen("hello")
    ?>

     2、对字符串中的单词计数

    <?php
    echo str_word_count("do you know this");
    ?>

     3、反转字符串

    <?php
    echo strrev("do you know this");
    ?>

     4、替换字符串中文本

    <?php
    echo str_replace("this","that","do you know this");
    ?>

     5、对数组排序函数

    sort() - 以升序对数组排序

    rsort() - 以降序对数组排序

    asort() - 根据值,以升序对关联数组进行排序

    ksort() - 根据键,以升序对关联数组进行排序

    arsort() - 根据值,以降序对关联数组进行排序

    krsort() - 根据键,以降序对关联数组进行排序

    例1:按字母升序/降序

    <?php
    $fruits=array("Banana","peach","apple");
    sort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

    <?php
    $fruits=array("Banana","peach","apple");
    rsort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

     例2:按数字升序/降序

    <?php
    $fruits=array(7,1,22,13);
    sort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

    <?php
    $fruits=array(7,1,22,13);
    rsort($fruits);
    $clength=count($fruits);
    for($x=0;$x<$clength;$x++)
    {
        echo $fruits[$x];
        echo "<br>";
    }
    ?>

     例3:根据值对数组进行升序/降序

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    asort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    arsort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

     例4:根据键对数组进行升序/降序

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    ksort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

    <?php
    $age=array("ann"=>"30","tian"=>"18","bill"=>"26");
    krsort($age);
    
    foreach($age as $x=>$x_value)
    {
        echo "key=" . $x . ", value=" . $x_value;
        echo "<br>";
    }
    ?>

  • 相关阅读:
    如何理解python中的if __name__=='main'的作用
    如何在阿里云上部署war包到tomcat服务器
    如何在windows上部署war包到tomcat服务器
    解决:mysql5.7 timestamp默认值0000-00-00 00:00:00 报错
    python3中的unicode_escape
    python中的excel操作
    python的logging模块
    python中的SMTP发送邮件
    python中的字符串
    一道问题引出的python中可变数据类型与不可变数据类型
  • 原文地址:https://www.cnblogs.com/tinkerbell/p/12733258.html
Copyright © 2011-2022 走看看