[英]How can I get my foreach loop to output the result as one combined array instead of separate ones?
我正在接收JSON輸入,我需要通過腳本運行它並獲取在新JSON數組中返回的值。 但是,當我這樣做時,它會產生例如3個單獨的數組,而不是收到的一個大數組。 例:
Input:
[{
"auth": "aa03e76d0a8bab11e08250000c29b481",
"topic": "edafdff398fb22847a2f98a15ca3186e/1",
"value": "1000"
},
{
"auth": "aa03e76d0a8bab11e08250000c29b481",
"topic": "edafdff398fb22847a2f98a15ca3186e/1",
"value": "2000"
},
{
"auth": "aa03e76d0a8bab11e08250000c29b481",
"topic": "edafdff398fb22847a2f98a15ca3186e/2",
"value": "3000"
}]
我通過以下腳本運行它:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
//Get inputs from request
$data = array();
$data = json_decode(file_get_contents('php://input'), true);
$reply_array = array();
//Start looping through data
foreach($data as $mid => $input) {
//Dig out the installation id and register
$identify = explode('/',trim($input['topic'],'/'));
$installation = preg_replace('/[^a-z0-9_]+/i','',array_shift($identify));
$RegisterID = array_shift($identify)+0;
// Extract the cleaning supplies needed to clean and validate the data
$sql_get_detergent="SELECT ProfileID, Description, Lvl4, Register, BitNo, DataFormat, Units, LowAct, HighAct, Multiplier, FaultCondition, FaultText FROM DeviceProfiles WHERE RegisterID = '".$RegisterID."' ORDER BY RegisterID ASC";
$result_get_detergent = mysqli_query($con,$sql_get_detergent);
while($row_clean = mysqli_fetch_array($result_get_detergent)) {
//Write out the reply
$semantic = strtolower($row_clean['Lvl4']);
$reply_array['topic'] = $installation."/".clean($semantic);
}
print_r(json_encode($reply_array));
}
輸出不是我需要的-如此處所示,它產生3個單獨的數組,而不是一個大數組。
Current output:
{"topic":"edafdff398fb22847a2f98a15ca3186e\/"}
{"topic":"edafdff398fb22847a2f98a15ca3186e\/"}
{"topic":"edafdff398fb22847a2f98a15ca3186e\/"}
Desired output:
[{
"topic":"edafdff398fb22847a2f98a15ca3186e\/"
},
{
"topic":"edafdff398fb22847a2f98a15ca3186e\/"
},
{
"topic":"edafdff398fb22847a2f98a15ca3186e\/"
}]
我究竟做錯了什么? 感謝您的幫助!
您將在foreach
循環中打印每個結果,並在循環的下一次迭代中覆蓋先前的結果。
當然,您會以這種方式獲得三個單一結果。
像這樣更改代碼:
$all_replies = array();
foreach($data as $mid => $input) {
// [...]
while($row_clean = mysqli_fetch_array($result_get_detergent)) {
$reply_array = array();
// Do you logic here - you can fill $reply_array as you want.
$all_replies[] = $reply_array;
}
}
print_r(json_encode($all_replies));
您還可以使用這種方法,這是一個較小的更改:
$reply_array = array();
foreach($data as $mid => $input) {
// [...]
while($row_clean = mysqli_fetch_array($result_get_detergent)) {
// [...]
$reply_array[]['topic'] = $installation . "/" . clean($semantic);
}
}
print_r(json_encode($reply_array));
這將適用於您的示例案例,但是您將無法在新任務中設置除topic
以外的其他值。 您必須采用以下方式:
$reply_array = array();
foreach($data as $mid => $input) {
// [...]
while($row_clean = mysqli_fetch_array($result_get_detergent)) {
// [...]
$reply_array[] = array(
'topic' => $installation . "/" . clean($semantic),
'auth' => $your_auth_value,
);
}
}
print_r(json_encode($reply_array));
我更喜歡第一個帶有額外變量的解決方案,但另一個解決方案也可以。
您的代碼中有一個小錯誤。
替換下面的代碼:
$reply_array['topic'] = $installation."/".clean($semantic);
與:
$reply_array[]['topic'] = $installation."/".clean($semantic);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.