Php 类stdClass的对象无法转换为laravel中的字符串
我正在尝试使用sql查询编写文本文件,但它显示一个错误,提示: 类stdClass的对象无法转换为字符串 这是我的密码:Php 类stdClass的对象无法转换为laravel中的字符串,php,mysql,laravel,Php,Mysql,Laravel,我正在尝试使用sql查询编写文本文件,但它显示一个错误,提示: 类stdClass的对象无法转换为字符串 这是我的密码: $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $ss=DB::select("SELECT commercial_library FROM tbl_schedule"); $stringData = implode(', ',$ss); fwrite($myfile, $string
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss=DB::select("SELECT commercial_library FROM tbl_schedule");
$stringData = implode(', ',$ss);
fwrite($myfile, $stringData);
fclose($myfile);
内爆将数组转换为字符串。我不认为
$ss
在你的例子中是一个数组,它看起来像一个对象。这可能就是出现此错误的原因。内爆将数组转换为字符串。我不认为$ss
在你的例子中是一个数组,它看起来像一个对象。这可能就是出现此错误的原因。将对象转换为数组,然后使用内爆。内爆将第一个参数作为数组
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss=DB::select("SELECT commercial_library FROM tbl_schedule");
$ss = (array) $ss;
$stringData = implode(', ', $ss);
fwrite($myfile, $stringData);
fclose($myfile);
将对象转换为数组,然后使用内爆。内爆将第一个参数作为数组
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss=DB::select("SELECT commercial_library FROM tbl_schedule");
$ss = (array) $ss;
$stringData = implode(', ', $ss);
fwrite($myfile, $stringData);
fclose($myfile);
您得到的类stdClass的对象在将$ss
转换为数组后无法转换为字符串,因为您仍然有一个对象数组。我试着用递归函数来解决你的问题。我有不同级别的stdclass和value。请用您的查询替换我对$ss
的定义,然后尝试我的代码
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss = new stdClass();
$ss->a = new stdClass();
$ss->a->a = new stdClass();
$ss->a->b = new stdClass();
$ss->b = new stdClass();
$ss->a->a->a = "!";
$ss->a->a->b = "T";
$ss->a->b->a = "E";
$ss->a->b->b = "X";
$ss->b->a = "T";
$ss->b->c = 1;
$string = recursive($ss, "");
fwrite($myfile, $string);
fclose($myfile);
function recursive($objects, $string) {
foreach($objects as $object) {
if ($object instanceof stdClass) {
$string = recursive((array) $object, $string);
} else {
$string .= implode(', ', (array) $object); //my example is to concatenate
}
}
return $string;
}
在将$ss
转换为数组后,无法将类stdClass的对象转换为字符串,因为您仍然有一个对象数组。我试着用递归函数来解决你的问题。我有不同级别的stdclass和value。请用您的查询替换我对$ss
的定义,然后尝试我的代码
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss = new stdClass();
$ss->a = new stdClass();
$ss->a->a = new stdClass();
$ss->a->b = new stdClass();
$ss->b = new stdClass();
$ss->a->a->a = "!";
$ss->a->a->b = "T";
$ss->a->b->a = "E";
$ss->a->b->b = "X";
$ss->b->a = "T";
$ss->b->c = 1;
$string = recursive($ss, "");
fwrite($myfile, $string);
fclose($myfile);
function recursive($objects, $string) {
foreach($objects as $object) {
if ($object instanceof stdClass) {
$string = recursive((array) $object, $string);
} else {
$string .= implode(', ', (array) $object); //my example is to concatenate
}
}
return $string;
}
内爆可以在数组上工作,您已经在一个对象上使用过它,它看起来像是DB select返回的对象,您正在尝试使用对象进行内爆。你们需要阵列来进行内爆。将您的对象转换为数组,然后使用内爆。当我删除错误时,我得到这个fwrite()期望参数2是字符串,数组给定的in爆在数组上工作,您在对象上使用过它似乎DB select返回对象,并且您正在尝试使用对象进行内爆。你们需要阵列来进行内爆。将您的对象转换为数组,然后使用内爆。当我删除错误时,我得到这个fwrite()期望参数2是字符串,给定的数组
类stdClass的对象无法转换为字符串
仍然收到此错误类stdClass的对象无法转换为字符串
仍然收到此错误当我删除错误时,我收到此fwrite()预期参数2为字符串,给定数组
当我删除错误时,我得到这个fwrite()期望参数2是字符串,给定数组