我在 php
中设置了一些数组$usersPostsDates = array();
$usersPostsNations = array();
$userpostsIds = array();
if (!in_array($jikuDate, $usersPostsDates)) {
array_push($usersPostsDates, $jikuDate);
}
if (!in_array($nation, $usersPostsNations)) {
array_push($usersPostsNations, $nation);
}
if (!in_array($idThisPost, $userpostsIds)) {
array_push($userpostsIds, $idThisPost);
}
然后在 js 我做:
var userDates = <?php echo json_encode($usersPostsDates); ?>;
var userNations = <?php echo json_encode($usersPostsNations); ?>;
var userIds = <?php echo json_encode($userpostsIds); ?>;
var i;
var a;
var b;
for (i = 0; i < userDates.length; ++i) {
$("#usp-custom-i"+i).attr("value", userDates[i]);
}
for (a = 0; a < userNations.length; ++a) {
$("#usp-custom-a"+a).attr("value", userNations[a]);
}
for (b = 0; b < userIds.length; ++b) {
$("#usp-custom-b"+b).attr("value", userIds[b]);
}
但是控制台给了我:
var userDates = ["1859","1858","1857"];
var userNations = ["United States"];
var userIds = [15221,3260,969];
为什么var userIds = [15221,3260,969]; isn't var userIds = ["15221","3260","969"];
?
如果我这样做:var_dump($userpostsIds);
我得到array(1) { [0]=> int(15221) }
array(2) { [0]=> int(15221) [1]=> int(3260) }
array(3) { [0]=> int(15221) [1]=> int(3260) [2]=> int(969) }
答案 0 :(得分:1)
因为如果您希望int(15221)
将数据转换为字符串,那么您的数据就像""
中所见的那样。
尝试以下操作:
array_push($userpostsIds, $idThisPost.'');