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>";
    }
    ?>

  • 相关阅读:
    IT开发者对Mac钟爱
    POJ 3486 &amp; HDU 1913 Computers(dp)
    基础排序算法
    LeetCode 70:Climbing Stairs
    Qt自己定义事件实现及子线程向主线程传送事件消息
    maven自己主动编译,解决你每次代码改动须要又一次编译的繁琐
    Unity定制 Image、Text的对象生成
    iOS学习4_UITableView的使用
    GTK+重拾--09 GTK+中的组件(一)
    Architecting Android…The clean way?
  • 原文地址:https://www.cnblogs.com/tinkerbell/p/12733258.html
Copyright © 2011-2022 走看看