数组值拼接成字符串
将所有数组值拼接成一个字符串
php自带的 implode 将数组拼接成一个字符串,这个方法有一个不足就是,数组的值不能是数组 只能是字符或者数字之类的基本类型 ,如果数组中的值也是一个数组的话,那么将会转换失败 。我在实际项目中遇到过这的需求,今天把我写好的代码分享给大家。希望,对需要的朋友有所帮助。
不足之处,敬请包涵。
<?php class Convert { public static function arrayConvertString( $array ,$join ) { $result = "" ; foreach ( $array as $k => $v ) { if( is_array( $v ) ) { $result .= $join. self::arrayConvertString( $v , $join ) ; } else { $result .= $join . $v ; } $result = preg_replace( "~^" . $join . "~" , "", $result ) ; } return $result ; } } // test $array = array( array(1,2,3,4,5),array("x1","x2","x3","x4","x5") ) ; $str = Convert::arrayConvertString( $array , ",") ; echo $str ; // echo:1,2,3,4,5,x1,x2,x3,x4,x5 ?>
以上就是数组值拼接成字符串的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment