簡體   English   中英

在foreach循環中合並數組

[英]merge array inside foreach loop

我想在foreach循環中合並數組結果。 是否有關於此或其他最佳方法的解決方案? 提前致謝。

PHP代碼

include('Config.php');
$data = json_decode(file_get_contents("php://input"));

foreach($data->dataVal as $key => $value){
 echo json_encode($config->query($key,$value));}

//this is the response code above --> how to merge this 2,3 array result into one only
[{"name":"Roi"}][{"name":"Tom"}][{"name":"Chad"}] 


//result to be expected like this 
[{"name":"Roi","name":"Tom","name":"Chad"}]  // this is i want

您將獲得的最接近的結果是,在foreach循環中構建數據數組,並對結果進行JSON編碼...

$result = array();
foreach($data->dataVal as $key => $value){
    $result[] = $config->query($key,$value);
}
echo json_encode($result);

這不再需要重復的名稱值。

在最初的嘗試中,您分別對每一行進行編碼,因此JSON無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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