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

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

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”转换为字符串或对象。 我使用了以下方法:

 &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; 
   
 
 

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

我传递的函数值是:

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drpkcwwav20524605 2018-05-25 16:09
    已采纳

    I managed to reproduce your "Array to string conversion" error when using the implode command by running the following line of code:

    implode(";", [[]]); // PHP Notice:  Array to string conversion in php shell code on line 1
    

    For converting a nested array into a string I found that a foreach loop worked:

    $nestedArray = ['outerKeyOne' => ['innerKeyOne' => 'valueOne'], 'outerKeyTwo' => ['innerKeyTwo' => 'valueTwo']];
    $arrayOfStrings = [];
    foreach ($nestedArray as $key => $value) {
        $arrayOfStrings[] = implode(",", $value);
    }
    implode(";", $arrayOfStrings); // string(17) "valueOne;valueTwo"
    

    The second error associated with the line $val = (object) $list; is from trying to embed an object into the $sql string. It seems like an object is not what you want here, unless it is an object that has a __toString() method implemented.

    I hope this is of some help. Using var_dump or something similar would provide more debug output to better diagnose the problems along with the above error messages. That's how I came up with the above code.

    已采纳该答案
    打赏 评论
  • dongzuoyue6556 2018-05-25 09:44

    You can use json_encode to convert Array to String:

    $FINAL_VALUE = json_encode($YOUR_OBJECT);
    

    For more information, you can refer this link.

    打赏 评论

相关推荐 更多相似问题