簡體   English   中英

有沒有更好的方法在 PHP 中打印出我的對象?

[英]Is there a better way to print out my object in PHP?

這是我現在得到的:

$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;
}

呼應:

179368 3A
176733 3B
686733 3C
178673 3D
168663 3E
937338 3F
936888 58
186335 59
199366 5A
159373 5B

到目前為止,我知道我可以使用for循環切換while使事情稍微短一些。 是否有另一種方法可以打印出看起來比這更好的某些行?

編輯:順便說一句,如果這意味着什么,我正在使用 Laravel。

編輯 2:那么有沒有一種方法可以在不知道列名稱的情況下打印出來?

謝謝!

更短的是使用foreach()進行迭代和使用printf()來回顯值。 您還可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM