Heim  >  Artikel  >  Backend-Entwicklung  >  php object或者array怎么转换成string

php object或者array怎么转换成string

WBOY
WBOYOriginal
2016-06-13 10:59:20926Durchsuche

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的定义来着,删掉就可以了
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