Heim  >  Artikel  >  Backend-Entwicklung  >  php中将数组转成字符串并保存到数据库中的函数代码_php技巧

php中将数组转成字符串并保存到数据库中的函数代码_php技巧

WBOY
WBOYOriginal
2016-05-17 08:55:02927Durchsuche
复制代码 代码如下:

/**
* 将字符串转换为数组
*
* @param    string  $data   字符串
* @return   array   返回数组格式,如果,data为空,则返回空数组
*/ 
 function string2array($data) {  
    if($data == '') return array();  
    @eval("\$array = $data;");  
    return $array;  
}  
 /**
* 将数组转换为字符串
*
* @param    array   $data       数组
* @param    bool    $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
* @return   string  返回字符串,如果,data为空,则返回空
*/ 
function array2string($data, $isformdata = 1) {  
    if($data == '') return '';  
    if($isformdata) $data = new_stripslashes($data);  
    return addslashes(var_export($data, TRUE));  

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn