感謝您看看我的問題,我開發了一個PHP自定義函數來處理Ajax,我基本上想知道一個值是否在數組中。爲什麼PHP說它的值不在數組中?
我在檢查event_try是否在數據庫查詢的結果集中。在這種情況下,1004在「$ reg」數組中(我保證它),在這種情況下,最後一個if-else應該返回「notok」,但從輸出帶有Ajax的json時總是會變得「OK」。
我已經完成了print_r的測試,它確實返回「notok」。這僅僅是出於某種原因與json和ajax。
下面的代碼:
function validate_event_no_repeat(){
// header('Content-type: application/json');
$check_repeat_result = 0;
$check_repeat = array();
$event_try = 1004;
global $wpdb;
$current_user = wp_get_current_user();
$get_attendee = $wpdb->get_results("SELECT ATT_ID FROM su_esp_attendee_meta WHERE ATT_email='" . $current_user->user_email . "'");
foreach ($get_attendee as $atte):
$loggedID = $atte->ATT_ID;
$registration = $wpdb->get_results("SELECT ATT_ID, EVT_ID FROM su_esp_registration WHERE ATT_ID = $loggedID", ARRAY_N);
foreach ($registration as $reg):
if(in_array($event_try, $reg)):
$check_repeat[] = "notok";
else:
$check_repeat[] = "ok";
endif;
endforeach;
endforeach;
if(in_array("notok", $check_repeat)):
$check_repeat_result = "notok";
else:
$check_repeat_result = "ok";
endif;
echo json_encode(array('crr' => $check_repeat_result));
die();
}
我知道上面是顯然不是天才完成,但它應該工作不應該嗎?我將不勝感激任何幫助,我可以得到這一點。
他用ARRAY_N作爲參數,我相信它產生一個數值數組,因此他使用in_array'的'沒有索引(我這裏居興) –
你預計'event_try'to是什麼?在'ATT_ID'或'EVT_ID'中? –
@bub它檢查EVT_ID –