The method of PHP array array common method, string operation

Array Function and Method Examples

1, whether there is a certain value the in_array () array is determined

    $arr = array('a','b','c');

    if(in_array('a',$arr)){

      echo "yes";

    }else{

      echo"no";

    } // output: yes

2, implode () array to a string

      $arr = array('a','b','c');

      echo implode (, $ arr "."); // output: abc

3, json_encode ()   Returns json format data

        $arr = array('a','b','c');

        echo json_encode($arr);

        返回  ["a","b","c"]

4, json_decode () JSON-formatted string is decoded and converted to PHP variable.

      $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

         var_dump(json_decode($json));

         var_dump(json_decode($json, true));

return:

      object(stdClass)[1]

        public 'a' => int 1

        public 'b' => int 2

        public 'c' => int 3

        public 'd' => int 4

        public 'e' => int 5

     array (size=5)

        'a' => int 1

        'b' => int 2

        'c' => int 3

        'd' => int 4

        'e' => int 5

 

5, Sort () to sort the array elements

  $arr = array('c','b','a');

  sort($arr);

  var_dump ($ arr);

  Abc return

6, array_push () is added to the array element

  $arr = array('a','b','c');

  array_push($arr,'d');

  var_dump ($ arr);

7, array_merge () the array merge

  $arr = array('a','b','c');

  $arr2 = array('1','2');

  var_dump(array_merge($arr,$arr2));

7, array_key_exists () determines whether a single array of keys

  $arr3 = array('a'=>'1','b'=>'2');

  var_dump(array_key_exists('a',$arr3));

  The return value is true

8, array_unique () to delete duplicate values ​​in array

  $arr4 = array('a','b','a');

  var_dump(array_unique($arr4));

  Back [ 'a', 'b' ]

9, array_shift () deletes the first element of the array

  $arr = array('a','b','c');

  array_shift($arr);

  var_dump ($ arr);

  Back [ 'b', 'c' ];

Examples of methods and operating string

1, Substr () String taken

  $str = "abcd";

  $a = substr($str,0,3);

  echo $ a;

  Abc return

2, Strlen () string length

  $str = "abcd";

  echo(strlen($str));

3, Strpos () to find the location of a character string of the first occurrence of

  $str = "abcd";

  echo(strpos($str,'b'));

  Return 1

4, str_replace () String replace

  $str = "abcd";

  echo (str_replace('b','2',$str));

  Return a2cd

5, Explode () array of string rotation  

  $str2 = "ab-cd";
  $a = explode('-',$str2);
  var_dump($a[1]);

  返回  cd的字符串

6、Strtoupper()  字符串转大写

  $str = "abcd";

  echo (strtoupper($str));

  返回ABCD

7、strtolower ()  字符串转小写

  $str3 = "ABCD";

  echo(strtolower($str3));

8、substr_replace()  字符串截取替换

  $str = "abcd";

  echo(substr_replace($str,'s',1));    

 

  返回as

strip_tags()

删除字符串中html标签

$str4 = "ab<a></a>cd";

echo(strip_tags($str4));

返回abcd

Trim()

删除空格回车等或其他定义字符

$str5 = "ab<br>cd";

echo(trim($str));

返回abcd

$str = "abcd";

echo(trim($str,'a'));

返回bcd

Guess you like

Origin www.cnblogs.com/cp123/p/9336981.html