我正在讀取一些 JSON 數據,并希望將其作為文本從函數返回。為此,我正在做以下事情。<?php $json = '{"Business":["Accounting","Macroeconomics"],"0":["English","English","Other","Penmanship"],"Math":["Calculus","Fractions","Functions"],"Other":["Geography","Philosophy"],"Science":["Geology","Physical Science"]}'; $json1 = json_decode($json, true); function rex($json){ foreach ($json as $key=>$value){ $x='<tr> <td style="width:30%;font-weight:700;padding-bottom:10px">'.$key.'</td> <td>'.implode(' ,',$value).'</td> </tr>'; echo $x; } } rex($json1); ?>我不知道如何從函數生成最終的連接字符串。如果我echo這樣做,那么它會返回我想要的內容,但如何在變量內捕獲該輸出。$x.=$x也沒有幫助。這些天我是 PHP 的初學者。
1 回答

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
return從函數和使用串聯。
...
function rex($json){
$x = '';
foreach ($json as $key => $value) {
$x .= '
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">'.$key.'</td>
<td>'.implode(', ',$value).'</td>
</tr>';
}
return $x;
}
echo rex($json1);
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消