【问题标题】:How to check if a php array value is in js array?如何检查php数组值是否在js数组中?
【发布时间】:2019-08-04 14:35:26
【问题描述】:

首先,我从php array 获得所有values

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

然后我得到那些arrays 并将它们转换为JS array

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

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

然后我需要检查当前id value 是否在js array 中并相应地继续:

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

这是在鼠标悬停在元素上时发生的,如果我在下面放置 id 是正确的,那么它与 this.id 无关

console.log(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);
});

【问题讨论】:

  • $.inArray 使用严格比较,确保this.id 与数组中的值类型相同。
  • @Titus 看起来我无法访问 gmap 中鼠标悬停时的 js 数组,就像我做 console.log(savedInternal); 一样,如果我将整个数组位放在 gmaps 中的悬停状态之前,我什么也得不到,我得到正确的值
  • $userPostsInternal 和 $userPostsExternal 是 php 数组吗?
  • 试试:$.inArray(this.id.toString(), savedInternal),其他检查也一样。
  • @FrançoisHuppé 确实

标签: javascript php jquery arrays


【解决方案1】:

$.inArray(...) 使用严格比较。 从您发布的代码看来,数组包含字符串(它们是使用 .split(...) 创建的,它返回一个字符串数组)以及您正在检查它是否在数组中的值(this.id)是一个数字。

要解决这个问题,请使用:

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

【讨论】:

  • 才华横溢,很有道理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多