zoukankan      html  css  js  c++  java
  • 自定义函数实现字符串数组互转

    自定义函数 实现字符串转数组

    <?php
    $str = 'I want to go home ';
    strToArr( $str );
    function strToArr( $str )
    {
        $arr = [];
        $s = '';
        for ( $i=0; $i < strlen( $str ); $i++) { 
            if( $str[$i] == ' ' )
            {
                $arr[] = $s;
                $s = '';
            }
            else
            {
                $s.= $str[$i];
            }
        }
        if( $s != '' && $s != ' ' )
        {
            $arr[] = $s;
        }
        return $arr; 
    }
    ?>
    

    自定义函数 实现数组转字符串

    <?php
    $arr = ['I', 'want', 'to', 'go','home'];
    echo arrToStr( $arr );
    function arrToStr( $arr )
    {
        $str = '';
        foreach ($arr as $key => $value) {
            if( $key == 0 )
            {
                $str = $value;
            }
            else
            {
                $str .= ' '.$value;
            }
        }
        return $str;
    }
    ?>
    
  • 相关阅读:
    forget word out4
    forget word out2
    forget words out1
    en_o out1
    en_e outtest2
    en_e out1
    疑难en_a
    en_a
    entest1
    铺音out2
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/11367928.html
Copyright © 2011-2022 走看看