rob.m
rob.m

Reputation: 10581

How to check if a php array value is in js array?

First of all I get all the values from a php array:

<?php
  $user_id = get_current_user_id();
  $userPostsInternal = get_user_meta( $user_id, 'save_post_internal', TRUE );
  $userPostsExternal = get_user_meta( $user_id, 'save_post_external', TRUE )
?>

I then get those arrays and convert them in a JS array

var savedInternal = "<?php echo $userPostsInternal; ?>";
var savedExternal = "<?php echo $userPostsExternal; ?>";

savedInternal = savedInternal.split(',');
savedExternal = savedExternal.split(',');

I then need to check if current id value is in the js array and proceed accordingly:

if($.inArray(this.id, savedInternal) !== -1) {
    console.log("yes");               
} else {
    console.log("no");  
}

This is happening on a mouse over an element, if I place the following the id is correct, so it isn't about this.id

console.log(this.id);

I get 128545 and it is correct.

Full code:

google.maps.event.addListener(circle, 'mouseover', function(e) {

  <?php
    $user_id = get_current_user_id();
    $userPostsInternal = get_user_meta( $user_id, 'save_post_internal', TRUE );
    $userPostsExternal = get_user_meta( $user_id, 'save_post_external', TRUE )
  ?>

  var savedInternal = "<?php echo $userPostsInternal; ?>";
  var savedExternal = "<?php echo $userPostsExternal; ?>";

  savedInternal = savedInternal.split(',');
  savedExternal = savedExternal.split(',');

  $("#timeSearch").removeClass("fadeIn").addClass("fadeOut");
  $(".infoBox").removeClass("fadeOut").addClass("fadeIn");
  if(this.currSite == "curr" ) {
    var linkGo = this.linkToPost;
    var whatSite = this.currSite;
    if($.inArray(this.id, savedInternal) !== -1) {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-outline-dark btn-block">Già nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    } else {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-dark btn-block">Salva nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    }
  } else { 
    var linkGo = linkExternal+this.linkToPost;
    var whatSite = this.currSite;
    if($.inArray(this.id, savedExternal) !== -1) {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-outline-dark btn-block">Già nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    } else {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-dark btn-block">Salva nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    }
  }
  infoWindow = new google.maps.InfoWindow({content: contentString});
  infoWindow.setPosition(this.getCenter());
  infoWindow.open(map);
  btnBoxSave(infoWindow, whatSite);
});

Upvotes: 0

Views: 85

Answers (1)

Titus
Titus

Reputation: 22474

$.inArray(...) uses strict comparison. From the code that you've posted it seems that the arrays contain strings (they were created using .split(...) which returns an array of strings) and the value you're checking to see if it is in the arrays (this.id) is a number.

To fix that, use:

$.inArray(this.id.toString(), savedInternal) and $.inArray(this.id.toString(), savedExternal)

Upvotes: 1

Related Questions