Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查php数组值是否在js数组中?_Javascript_Php_Jquery_Arrays - Fatal编程技术网

Javascript 如何检查php数组值是否在js数组中?

Javascript 如何检查php数组值是否在js数组中?,javascript,php,jquery,arrays,Javascript,Php,Jquery,Arrays,首先,我从php数组中获取所有值: 然后我需要检查当前id值是否在js数组中,并相应地继续: if($.inArray(this.id, savedInternal) !== -1) { console.log("yes"); } else { console.log("no"); } 这是在一个元素上的鼠标上发生的,如果我放置以下id是正确的,那么它与This.id无关 我得到128545,这是正确的 完整代码: google.maps.ev

首先,我从php数组中获取所有值:

然后我需要检查当前id值是否在js数组中,并相应地继续:

if($.inArray(this.id, savedInternal) !== -1) {
    console.log("yes");               
} else {
    console.log("no");  
}
这是在一个元素上的鼠标上发生的,如果我放置以下id是正确的,那么它与This.id无关

我得到128545,这是正确的

完整代码:

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);
});
使用严格的比较。 从您发布的代码来看,数组似乎包含使用.split创建的字符串。。。它返回一个字符串数组和您正在检查的值,以查看该值是否在数组中。this.id是一个数字

要解决此问题,请使用:


$.inaraythis.id.toString、savedInternal和$.inaraythis.id.toString、savedInternal

$。inaray使用严格的比较,确保this.id与数组中的值的类型相同。@Titus看起来我无法在gmap中鼠标悬停时访问js数组,就像我访问console.logsavedInternal一样;如果我将整个数组位放在gmap中悬停状态之前,我将得到正确的值$userPostsInternal和$userpostseternal是php数组?试试:$.inaraythis.id.toString,savedInternal,对另一个检查也做同样的操作。@FrançoisHuppéyes indeed很多,完全有意义
if($.inArray(this.id, savedInternal) !== -1) {
    console.log("yes");               
} else {
    console.log("no");  
}
console.log(this.id);
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);
});