我已经花了好几个小时试图在数组中赋值但是每次迭代都将数组设置为0而我无法理解为什么,这里是代码......
$AurQ = array();
$prod = '';
while ($NOTIFY = $notification->fetch_assoc()) {
print_r($AurQ);
if ($NOTIFY['notification_type_ID'] == 1) {
if (in_array($NOTIFY['qID'], $AurQ, true)) {
echo "exist";
continue;
} else {
$AurQ[] = $NOTIFY['qID']; // Adding the value to the array
$prod .= '<li><a href="http://localhost/website/link/' . $NOTIFY['qID'] . '"> <span class="AskIcon"></span><span class="noti_announce">';
$prod .= '<span class="unB">' . $NOTIFY['first_name'] . ' ' . $NOTIFY['last_name'] . '</span> ' . $NOTIFY['notify_name_en'] . ' "' . $NOTIFY['q_title'] . '"</span>';
$prod .= '<span class="noti_time"><span class="icon"></span>' . time_elapsed_string($NOTIFY['time']) . '</span></a></li>';
} // end of if doesn't exist in Array List
} //end of if
} // end of loop
答案 0 :(得分:0)
问题似乎是,只有在检查了该阵列中是否已存在之后,您才会将值推送到数组中,但是它并没有给出您#39 ;将其初始化为正上方的空数组。
因此,此处存在逻辑错误,并且根据您尝试执行的操作,可以通过将$AurQ[] = $NOTIFY['qID'] ;
行移至else
语句来修复此问题,或者将if语句从if (in_array($NOTIFY['qID'], $AurQ, true)) {
更改为if (!in_array($NOTIFY['qID'], $AurQ, true)) {
。
正如你没有提到当你不想要它们时所显示的事物的问题时,我会假设你正在寻找前一种解决方案,而不是后一种解决方案。 。所以,你的代码最终会看起来像这样:
$AurQ = array();
$prod ='';
while($NOTIFY = $notification->fetch_assoc()) {
if ($NOTIFY['notification_type_ID'] == 1) {
if (in_array($NOTIFY['qID'], $AurQ, true)){ //If it's ALREADY in the array
echo "exist";
continue;
} else { //Otherwise we need to add it, and display it
$AurQ[] = $NOTIFY['qID'] ; // Adding the value to the array
$prod .= '<li><a href="http://localhost/website/link/'.$NOTIFY['qID'].'"> <span class="AskIcon"></span><span class="noti_announce">';
$prod .= '<span class="unB">'.$NOTIFY['first_name'].' '.$NOTIFY['last_name'].'</span> '.$NOTIFY['notify_name_en'].' "'.$NOTIFY['q_title'].'"</span>';
$prod .= '<span class="noti_time"><span class="icon"></span>'.time_elapsed_string($NOTIFY['time']).'</span></a></li>';
}// end of if doesn't exist in Array List
}//end of if
} // end of loop
答案 1 :(得分:0)
鉴于您的代码,您只需将function calcualteMonthYr(){
var fromDate =new Date($('#txtDurationFrom2').val()); // Date picker (text fields)
var toDate = new Date($('#txtDurationTo2').val());
var months=0;
months = (toDate.getFullYear() - fromDate.getFullYear()) * 12;
months -= fromDate.getMonth();
months += toDate.getMonth();
if (toDate.getDate() < fromDate.getDate()){
months--;
}
$('#txtTimePeriod2').val(months); // result
}
移至else块即可。
目前,$AurQ[] = $NOTIFY['qID'];
为空,如果阻止将永远不会运行,因为$AurQ[]
将始终返回false。