基本上,我需要以非常特定的方式輸出電子郵件列表,以便 SendGrid API 能夠理解。它看起來像這樣。{email:[email protected]},{email:[email protected]},{email:[email protected]}本質上,我只需要添加大括號和電子郵件:,我不知道該怎么做。我的代碼是這樣的。$e= array("[email protected]", "[email protected]", "[email protected]");foreach ($e as $x) { echo"{email:$x}, <br>";}我使用 echo 是因為當我嘗試將其轉換為 var 時,它要么給我一個錯誤代碼,要么只顯示 $e 數組中的最后一封電子郵件。為什么只為數組中的每個項目添加一些東西就這么難?
2 回答

楊魅力
TA貢獻1811條經驗 獲得超6個贊
這對我有用...
$e= array("[email protected]", "[email protected]", "[email protected]");
foreach ($e as $x) {
$y[] = "{email:$x}";
}
$list = implode(",<br>", $y);
//print_r($list);

精慕HU
TA貢獻1845條經驗 獲得超8個贊
有效的 JSON:
$e= array("[email protected]", "[email protected]", "[email protected]");
$final_arr = [];
foreach ($e as $x) {
$final_arr[]['email'] = $x;
}
/*
echo json_encode($final_arr); results :-
[{"email":"[email protected]"},
{"email":"[email protected]"},
{"email":"[email protected]"}]
*/
- 2 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消