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

时间:2016-06-03 13:23:08

标签: php jquery json ajax comparison

所以,我有if statement检查$chosenCourseId是否等于$thisCertificateCourseId或者它是否等于0

0相比,我认为它是正确的,但如果$chosenCourseId等于任何其他数字,则它与$thisCertificateCourseId不匹配,即使它们是2740 == 2740相同的$thisCertificateCourseId = 2740

对PHP代码进行比较,回显到Ajax并显示该课程的证书数量(两个日期之间)。

问题是,如果我只运行PHP代码,它会回显数字本身console.log(),但当我NULL时,它会返回$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()

PHP代码$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++; }

console.log()

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

$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) 

PHP代码自行运行的结果:

$chosenCourseId

编辑:

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

$thisCertificateCourseId2740strcmp($chosenCourseId, $thisCertificateCourseId)时: -47返回2740

此语句永远不会触发(我知道if($thisCertificateCourseId == 2740 OR $thisCertificateCourseId == '2740') { $certificateCounter++; } 是数字/字符串之一):

if(true){
    $certificateCounter++;
}

如果我尝试下面的代码,它可以工作,但我需要知道该证书是否来自用户选择的课程。

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

有关如何解决这个问题的想法吗?

filterDashboard()返回代码:

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\";}

返回:

{{1}}

1 个答案:

答案 0 :(得分:1)

我明白了。 这里的事情是因为我通过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++;
    }

}