我是PHP脚本的新手,我试图在PHP中迭代foreach循环我只得到第一个值结果循环不会转到数组中的下一个元素。
// Parsing data
$Id = htmlentities($_POST["Id"]);
$tables = array("Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday") ;
foreach ($tables as $value) {
//Call CheckData function
$result = checkData($value ,$Id , $conn);
if(!empty($result)) {
$returnArray["status"] = true ;
$returnArray["message"] = $result ;
$returnArray["Day of Week"] = $value ;
echo json_encode($returnArray) ;
} else {
$returnArray["status"] = false ;
$returnArray["message"] = "No data found for ' $value'";
$returnArray["Day of Week"] = $value ;
echo json_encode($returnArray) ;
}
}
return ;
我星期一的结果不是星期二,依此类推。
// CheckData的代码
function checkData($value ,$Id , $conn) {
$returnValue[] = array() ;
//SQL query
$sql = "SELECT * FROM $value WHERE Id = '$Id'" ;
//Store the result in $result
$result = mysqli_query($conn , $sql);
while ($row = mysqli_fetch_array($result)) {
$returnValue[] = array_map(utf8_encode ,$row);
}
return $returnValue ;
}
答案 0 :(得分:0)
//Parsing data
$Id = htmlentities($_POST["Id"]);
$tables = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") ;
$returnArray = [];
$intCount = 0;
foreach ($tables as $value) {
//Call CheckData function
$result = checkData($value ,$Id , $conn);
if (!empty($result)) {
$returnArray[$intCount]["status"] = true ;
$returnArray[$intCount]["message"] = $result ;
$returnArray[$intCount]["Day of Week"] = $value ;
echo json_encode($returnArray) ;
} else {
$returnArray[$intCount]["status"] = false ;
$returnArray[$intCount]["message"] = "No data found for ' $value'";
$returnArray[$intCount]["Day of Week"] = $value ;
echo json_encode($returnArray) ;
}
$intCount++;
}
return ;