-2

I have set some arrays in 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);
}

Then in js I do:

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]);
}

But console gives me:

var userDates = ["1859","1858","1857"];
var userNations = ["United States"];
var userIds = [15221,3260,969];

Why var userIds = [15221,3260,969]; isn't var userIds = ["15221","3260","969"]; ?

If I do: var_dump($userpostsIds); I get array(1) { [0]=> int(15221) } array(2) { [0]=> int(15221) [1]=> int(3260) } array(3) { [0]=> int(15221) [1]=> int(3260) [2]=> int(969) }

rob.m
  • 9,843
  • 19
  • 73
  • 162

1 Answers1

1

Because your data is int as seen in int(15221) if you want to have it in "" convert it to string.

try doing below:

array_push($userpostsIds, $idThisPost.'');
guradio
  • 15,524
  • 4
  • 36
  • 57