>  기사  >  백엔드 개발  >  php object或者array怎么转换成string

php object或者array怎么转换成string

WBOY
WBOY원래의
2016-06-13 10:59:20923검색

php object或者array如何转换成string
做php数据库查询的时候有两个函数
oci_fetch_object和oci_fetch_array(),分别返回object和array类型的返回值,我现在需要一个string类型的,如何转换?


$results = "[";
$row = oci_fetch_array($stmt);    $results += ???   $results = "]";

------最佳解决方案--------------------
while($row = oci_fetch_array($stmt)) {
 $result[] = $row;
}
$result = json_encode($result);
------其他解决方案--------------------
你可能需要的是 json_encode

------其他解决方案--------------------

引用:
你可能需要的是 json_encode

我试过,不行。。。
其实我的目的就是要把oci查询的返回值变成json串
------其他解决方案--------------------
看你的格式,你是想把object或者是array转化为json格式吧。。
楼上正解,转化为json是json_encode,json转为array是json_decode
如果想自己拼接也可以啊。数组转化为string,直接用implode好了
------其他解决方案--------------------
贴出楼主的数据
------其他解决方案--------------------
$results = "[";
$usercount = 0;
$row = oci_fetch_array($stmt);
if($row){
$results += $row;
$usercount++;
}
while (1) {
$row = oci_fetch_array($stmt);
if(!$row) break;
    
    $results += ","+$row; 
    $usercount++;
}
$results += "]"; 

这个是我的代码,得出的$results是0而不是json数组,其中查询是有多条返回值的
------其他解决方案--------------------
引用:
while($row = oci_fetch_array($stmt)) {
 $result[] = $row;
}
$result = json_encode($result);

这样改后提示Cannot use a scalar value as an array in,然后$result=“”
------其他解决方案--------------------
你把你原来的 $results = "["; 删掉啊!
------其他解决方案--------------------
引用:
你把你原来的 $results = "["; 删掉啊!

删掉了,只写
while($row = oci_fetch_array($stmt)) {
 $result[] = $row;
}
$result = json_encode($result);
这一段来着,然后返回$result

 $result[] = $row;提示Cannot use a scalar value as an array in
------其他解决方案--------------------
引用:
你把你原来的 $results = "["; 删掉啊!


可以了,刚刚发现在函数开始有个$result的定义来着,删掉就可以了
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.