请问这是什么格式的数据?

WBOY
Release: 2016-06-23 13:54:51
Original
941 people have browsed it

a:4:{i:0;i:392;i:1;i:393;i:2;i:394;i:3;i:395;}
这是从数据库读出来的 。判断这个is_array 返回1 
说是个数组类型。这怎么才能转换成string或者给JS用呢。。谢谢


回复讨论(解决方案)

这是 php 序列化(serialize)数据
用 is_array 判断不可能返回 1
除非你做了反序列化(unserialize)

$a = unserialize('a:4:{i:0;i:392;i:1;i:393;i:2;i:394;i:3;i:395;}');echo '[' . join(',', $a) . ']';//或echo json_encode($a);
Copy after login
[392,393,394,395]
就可以给 js 用了

1、序列化的数据先使用unserialize反序列化,得到原始的数组;
2、如果要将PHP里的数组传给JS用,那得转成JSON,然后JS访问JSON数据

2L解答正确。不过貌似这个只能赋值给session
我直接unserialize报错。

附代码。谢谢

if($action == 'getproductsname'){		$works_id = isset($_GET["works_id"])?intval($_GET["works_id"]):0;	$tmp = $db->selectAssoc('*','works','works_id',$works_id);	if($tmp == false){		exit('读取数据库失败!');	}	$info = $tmp[0];unset($tmp);	ds('worksid');	rs('worksid',unserialize($info['works_pics']));	$imageid = gs('worksid');	if(is_array($imageid))$tmp = implode(',',$imageid); //将array 转换成string		if(isset($tmp)){		$sql = "select * from pics where id in(".$tmp.") order by id asc";		$info = $db->qa($sql);		if($info != false){			$ct = count($info);			for($i=0;$i<$ct;$i++){				//echo '<img  src="'.BASE_DIR.'/'.$offlineindex.'/'.$info[$i]['id'].'.jpg" / alt="请问这是什么格式的数据?" ><span   style="max-width:90%" onclick="delimages('.$info[$i]['id'].');">删除</span>';				echo '<option value="'.$info[$i]['id'].'" >'.$info[$i]['title'].'</option>';			}		}else{echo '请上传图片!';}	}		}
Copy after login

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!