douliang2935
2018-05-25 07:37
浏览 742
已采纳

将数组转换为字符串或对象

Good day, I am trying to convert an array "$list" into string or object. I have used following methods:

<?php

include "medclass.php";

session_start();
if (isset($_SESSION['mail'])) 
{
    $list = $_SESSION['basket'];
}
else
header("location: clientsigninpage.php?msg= Log-in First");

$obj = new med_class;
$obj->connectdb();

$val = implode(";",$list);    //implode method
$val = (object) $list;        //object method
$val = serialize($list);      //serialize method

$result = $obj->searchMed($val);

while ($row = $result->fetchObject()) 
{
  echo $row->MedPrice;
}

?>

With "(object)" its giving me following error: "Object of class stdClass could not be converted to string", with "implode": "Array to string conversion" and with "serialize()" it does not print anything.

The function that I am passing value is:

function searchMed($v1)
    {
        $sql = "select * from storepreview where MedName = '$v1'";
        $ret = $this->con->query($sql);
        return $ret;
    }

I have used these methods by seen following links: (http://www.dyn-web.com/php/arrays/convert.php) ; (Convert an array to a string); (How to convert an array to object in PHP?)

图片转代码服务由CSDN问答提供 功能建议

美好的一天,我正在尝试将数组“$ list”转换为字符串或对象。 我使用了以下方法:</ p>

 &lt;?php 
 
include“medclass.php”; 
 
session_start(); 
if(isset($ _ SESSION ['  mail']))
 {
 $ list = $ _SESSION ['basket']; 
} 
else 
header(“location:clientsigninpage.php?msg = Log-in First”); 
 
 $  obj = new med_class; 
 $ obj-&gt; connectdb(); 
 
 $ val = implode(“;”,$ list);  // implode方法
 $ val =(object)$ list;  //对象方法
 $ val = serialize($ list);  //序列化方法
 
 $ result = $ obj-&gt; searchMed($ val); 
 
而($ row = $ result-&gt; fetchObject())
 {
 echo $ row-&gt;  MedPrice; 
} 
 
?&gt; 
 </ code> </ pre> 
 
 

使用“(object)”它给出了以下错误:“类stdClass的对象无法转换 to string“,with”implode“:”Array to string conversion“和”serialize()“它不会打印任何内容。</ p>

我传递的函数值是:</ p>

  function searchMed($ v1)
 {
 $ sql =“select * from storepreview medName ='$ v1'”; 
 $ ret = $ this-&gt; 接受查询($ sql); 
返回$ ret; 
} 
 </ code> </ pre> 
 
 

我通过以下链接使用了这些方法:( http://www.dyn-web.com/php/arrays/convert.php </ a>); (将数组转换为字符串); (如何在PHP中将数组转换为对象?</ a >)</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题