[英]Is there a better way to print out my object in PHP?
Here's what I got right now:这是我现在得到的:
$results = DB::select('select * from test_table');
$result= json_decode(json_encode($results), true);
$x=0;
try {
while ($x<10)
{
echo $result[$x]['column_A'];
echo " ";
echo $result[$x]['column_B'];
echo "<br>";
$x++;
}
}
catch(exception $e) {
echo $e;
}
which echos out:呼应:
179368 3A
176733 3B
686733 3C
178673 3D
168663 3E
937338 3F
936888 58
186335 59
199366 5A
159373 5B
So far I know that I could switch out while
with a for
loop to make things slightly shorter.到目前为止,我知道我可以使用
for
循环切换while
使事情稍微短一些。 Is there another method of printing out certain rows that looks better than this?是否有另一种方法可以打印出看起来比这更好的某些行?
EDIT: By the way, I'm using Laravel if that means anything.编辑:顺便说一句,如果这意味着什么,我正在使用 Laravel。
EDIT 2: So is there a way I could print this out without knowing the names of the columns?编辑 2:那么有没有一种方法可以在不知道列名称的情况下打印出来?
Thanks!谢谢!
Shorter would be with the use of foreach()
for iterating and printf()
for echo'ing the values.更短的是使用
foreach()
进行迭代和使用printf()
来回显值。 You can also use sprintf()
to store the string into a variable.您还可以使用
sprintf()
将字符串存储到变量中。
$results = DB::select('select * from test_table');
$result= json_decode(json_encode($results), true);
foreach ($result as $row) {
printf('%s %s<br/>', $row['column_A'], $row['column_B']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.