Home > Backend Development > PHP Tutorial > JS/PHP中,数组与字符串的转换

JS/PHP中,数组与字符串的转换

PHPz
Release: 2018-09-30 15:06:53
forward
1328 people have browsed it

这个数组和字符串的相互转换的API一直以来我就老是记不明白,这两天把他们都凑合在一起,才慢慢清晰,直至记下来,我相信不只我一个人对它们很无奈,那我们今天就来一起看看吧,好好地识别一下它们~

JS

数组——字符串join()

每个元素之间以-(各种符号)来分割成字符串,默认以逗号分割,返回分割后的字符串

var arr = [1,2,3,4,5];var res = arr.join("-");console.log(res);//1-2-3-4-5
Copy after login

 

 

字符串——数组split()

每个元素之间以,分割成几部分,返回数组

var str = "hello,how are you";
var arr = str.split(",");
console.log(arr);//["h", "e", "l", "l", "o", ",", "h", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u"]
Copy after login

PHP

数组——字符串implode()

把数组添加间隔符转换为字符串

$arr = [1,2,3,4,5];$str = implode("*",$str);echo $str;//1*2*3*4*5
Copy after login

字符串——数组explode()

把字符串从规定位置转换为数组

$str = "dsa-das-fsaf-dfsg";$arr = explode("-",$str);print_r($arr);//Array([0]=>dsa[1]=>das[2]=>fsaf[3]=>dfsg)
Copy after login

 

 

 

 

 数组——JSON字符串json_encode()

JSON字符串——数组/对象json_decode($atr,true/false)

这回可要记住了哦,提醒:可以运用自己使用它们的某次难忘的经历,也可以使用别的一些巧妙的方法去记忆它们,加油加油加油!

更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template