简体   繁体   English

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

[英]Convert an array into string or object

Good day, I am trying to convert an array "$list" into string or object. 美好的一天,我试图将数组“ $ list”转换为字符串或对象。 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. 使用“(对象)”,会给我以下错误:“类stdClass的对象无法转换为字符串”,使用“爆破”:“数组到字符串的转换”,使用“ serialize()”,它不会打印任何内容。

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 ) ; 我通过查看以下链接使用了这些方法:( http://www.dyn-web.com/php/arrays/convert.php ); ( Convert an array to a string ); 将数组转换为字符串 ); ( How to convert an array to object in PHP? ) 如何在PHP中将数组转换为对象?

You can use json_encode to convert Array to String: 您可以使用json_encode将Array转换为String:

$FINAL_VALUE = json_encode($YOUR_OBJECT);

For more information, you can refer this link . 有关更多信息,您可以参考此链接

I managed to reproduce your "Array to string conversion" error when using the implode command by running the following line of code: 通过运行以下代码行,在使用implode命令时,我设法重现了“数组到字符串转换”错误:

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: 要将嵌套数组转换为字符串,我发现一个foreach循环有效:

$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; 与第二行有关的第二个错误$val = (object) $list; is from trying to embed an object into the $sql string. 是因为尝试将对象嵌入$sql字符串。 It seems like an object is not what you want here, unless it is an object that has a __toString() method implemented. 似乎对象不是您想要的,除非它是已实现__toString()方法的对象。

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. 使用var_dump或类似的东西将提供更多的调试输出,以更好地诊断问题以及上述错误消息。 That's how I came up with the above code. 这就是我想到的上述代码的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM