gpt4 book ai didi

php - 为什么我不能在 PHP 中比较这些数字?

转载 作者:行者123 更新时间:2023-12-01 04:41:05 24 4
gpt4 key购买 nike

因此,我有一个 if 语句,用于检查 $chosenCourseId 是否等于 $thisCertificateCourseId 或者是否等于 0.

当它与 0 进行比较时,我明白了,但如果 $chosenCourseId 等于任何其他数字,则它与 $thisCertificateCourseId< 不匹配,即使它们是相同的 2740 == 2740

比较发生在 PHP 代码上,该代码回显到 Ajax 并显示该类(class)的证书数量(两个日期之间)。

问题是,如果我只运行 PHP 代码,它会回显数字本身 $thisCertificateCourseId = 2740 但当我 console.log() 它时,它会返回 NULL

你能告诉我我做错了什么吗?

PHP 代码:

$certificateCounter = 0;
foreach ($certificates as $certificate) {

$thisCertificateCourseId = unserialize($certificate['certificate_course_id'])[0];
$thisCertificateTimestamp = date("Y-m-d", strtotime($certificate['time_stamp']));

if($thisCertificateTimestamp >= $minDate
AND $thisCertificateTimestamp <= $maxDate
AND ($chosenCourseId == $thisCertificateCourseId OR $chosenCourseId == 0)
) {

$certificateCounter++;

}

}

PHP 代码到 console.log():

$certificateCounter = 0;
$icounter = 0;
foreach ($certificates as $certificate) {

$thisCertificateCourseId = unserialize($certificate['certificate_course_id'])[0];
$thisCertificateTimestamp = date("Y-m-d", strtotime($certificate['time_stamp']));

$certificateCourseId[$icounter] = $thisCertificateCourseId;
$icounter++;

}

jQuery 到 console.log():

jQuery.each(data[10], function(index, value) {
console.log(index + ' -> ' + value);
});

PHP 代码自行运行:

$chosenCourseId = 2740;

$certificateCounter = 0;
foreach ($certificates as $certificate) {

$thisCertificateCourseId = $certificate['certificate_course_id'];
$thisCertificateTimestamp = date("Y-m-d", strtotime($certificate['time_stamp']));

echo 'Raw: ';
var_dump($thisCertificateCourseId);
echo "<BR>";
echo "Unserialized: ";
var_dump(unserialize($thisCertificateCourseId));
echo "<BR>";
echo "[0]: ";
var_dump(unserialize($thisCertificateCourseId)[0]);
echo "<BR>";
echo "Is equal? ";
var_dump(unserialize($thisCertificateCourseId)[0] == $chosenCourseId);
echo "<BR><BR>";

}

PHP代码自行运行的结果:

Raw: string(51) "a:3:{i:0;s:4:"1139";i:1;s:4:"2968";i:2;s:4:"2968";}" 
Unserialized: array(3) { [0]=> string(4) "1139" [1]=> string(4) "2968" [2]=> string(4) "2968" }
[0]: string(4) "1139"
Is equal? bool(false)

Raw: string(21) "a:1:{i:0;s:4:"1139";}"
Unserialized: array(1) { [0]=> string(4) "1139" }
[0]: string(4) "1139"
Is equal? bool(false)

Raw: string(51) "a:3:{i:0;s:4:"2740";i:1;s:4:"2968";i:2;s:4:"2968";}"
Unserialized: array(3) { [0]=> string(4) "2740" [1]=> string(4) "2968" [2]=> string(4) "2968" }
[0]: string(4) "2740"
Is equal? bool(true)
<小时/>

编辑:

所以我发现变量 $chosenCourseId$thisCertificateCourseId 都是带有 is_string() 的字符串,它返回了我 true。另一方面,is_numeric() 对两个变量都返回了 false

$chosenCourseId$thisCertificateCourseId2740时:strcmp($chosenCourseId, $thisCertificateCourseId) 返回我-47

此语句永远不会触发(我知道 2740 是数字/字符串之一):

if($thisCertificateCourseId == 2740 OR $thisCertificateCourseId == '2740') {
$certificateCounter++;
}

如果我尝试下面的代码,它会起作用,但我需要知道该证书是否来自用户选择的类(class)。

if(true){
$certificateCounter++;
}

关于如何解决这个问题有什么想法吗?

<小时/>

filterDashboard() 返回代码:

return array(
$courseList, // 0
$unitList, // 1
$certificatesList, // 2
$quizScoreList, // 3
$authorList, // 4
$authorsPosition, // 5
$authorsPercent, // 6
$unitSeconds, // 7
$totalSecondsConverted, // 8
$certificateCounter, // 9
$certificateCourseId, // 10
);

echo json_encode(filterDashboard($units, $certificates, $quizzes, $minDate, $maxDate, $authorsId, $courseOf, $chosenCourseId, $chosenUnitId));

jQuery.each(data, function(index, value) {
console.log(index + ' -> ' + value);
});

返回:

0 -> [object Object],[object Object]
1 -> [object Object],[object Object],[object Object],[object Object]
2 ->
3 ->
4 -> [object Object],[object Object],[object Object]
5 -> 1
6 -> 100,00
7 -> [object Object]
8 -> 34min 3s
9 -> 0
10 -> a:3:{i:0;s:4:\"1139\";i:1;s:4:\"2968\";i:2;s:4:\"2968\";},a:1:{i:0;s:4:\"1139\";},a:3:{i:0;s:4:\"2740\";i:1;s:4:\"2968\";i:2;s:4:\"2968\";},a:3:{i:0;s:4:\"2968\";i:1;s:4:\"2968\";i:2;s:4:\"2968\";},a:2:{i:0;s:4:\"2968\";i:1;s:4:\"2968\";}

最佳答案

我明白了。这里的问题是因为我通过 ajax/json 传递一个数组,并且在 PHP 端它向序列化数组添加了斜杠。

在反序列化字符串之前,我使用 stripcslashes() 解决了这个问题。

遵循工作代码:

$certificateCounter = 0;
foreach ($certificates as $certificate) {

$thisCertificateCourseId = unserialize(stripcslashes($certificate['certificate_course_id']))[0];
$thisCertificateTimestamp = date("Y-m-d", strtotime($certificate['time_stamp']));

if($thisCertificateTimestamp >= $minDate
AND $thisCertificateTimestamp <= $maxDate
AND ($chosenCourseId == $thisCertificateCourseId OR $chosenCourseId == 0)
) {
$certificateCounter++;
}

}

关于php - 为什么我不能在 PHP 中比较这些数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37615600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com