简体   繁体   English

将JS变量等同于json编码的php数组值,而不管索引如何

[英]Equating JS variable to json encoded php array value regardless of index

I'm comparing a JS variable to a json encoded php array value, which is working currently as is: 我正在将JS变量与json编码的php数组值进行比较,该值目前按原样工作:

function myFunction() {
    var itemArray = <?php echo json_encode($items);?>;   
    console.log(itemArray);

    var number = 14;

    if(number == itemArray[0]['number']){
        success.style.display = "block";
        error.style.display = "none";
    }
}

[{
    0:
        name: "nameOne"
        number: 14
    1:
        name: "nameTwo"
        number: 13
    2:
        name: "nameThree"
        number: 8
}]

$items: array:3 [
    0=>array{
        name: "nameOne"
        number: 14
    }
    1=>array{
        name: "nameTwo"
        number: 13

    }
    2=>array{
        name: "nameThree"
        number: 8
    }
]

So It's indexed to zero which is fine for testing the comparison, but this isn't what I want. 因此它被索引为零,这对于测试比较来说是很好的,但这不是我想要的。 I want to be able to look at the 'number' field of every index in this object. 我希望能够查看此对象中每个索引的“数字”字段。 So basically I want to say "If 14 exists as 'Number' in any of the indeces here, then this should be true". 因此,基本上我想说“如果在这里的任何索引中14作为'Number'存在,那么这应该是正确的”。

I want to do this for other fields eventually too, but when I change my index to if(number == itemArray[]['number']){ then it says the function is undefined. 我最终也想对其他字段执行此操作,但是当我将索引更改为if(number == itemArray[]['number']){它表示该函数未定义。

How can I do this for any index in the object to look at that field? 我如何才能使对象中的任何索引查看该字段?

If you just want to array an array of objects that match the key try this. 如果您只想排列与键匹配的对象数组,请尝试此操作。 The 2nd function returns an array of the matching indices, although there is probably a better way to do that. 第二个函数返回匹配索引的数组,尽管可能有更好的方法。

 var array = [{ name: "nameOne", number: 14 },{ name: "nameTwo", number: 13 },{ name: "nameThree", number: 8 },{ name: "nameThree", number: 14 } ]; function findMatches(array, key, value) { return array.filter(item => item[key] == value); } function findIndex(array, key, value) { return array.map((item,index) => item[key] === value ? index : undefined).filter(x => x !== undefined); } console.log(findMatches(array, "number", 14)); console.log(findIndex(array, "number", 14)); console.log(findMatches(array, "number", 13)); console.log(findIndex(array, "number", 13)); console.log(findMatches(array, "number", 2)); console.log(findIndex(array, "number", 2)); 

<?php
//I suppose Your items will be like
$items = 
array(array("name" => "nameOne","number" => 14),
    array("name"=> "nameTwo","number"=> 13),
     array("name"=> "nameThree","number"=> 8)
);

echo "<pre>";
print_r($items);
?>

<script>
myFunction();
function myFunction() {
  //Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
   var json  = JSON.parse('<?php echo json_encode($items);?>');   
   console.log(json );

   var number = 14;
   var result = false;
   var error = '';
   //Use the loop to get the value from json
  for(var i = 0; i < json.length; i++) {
    var obj = json[i];

   //If obj.number is 14 add your code
   if(number == obj.number){
      result = true;

   }
  }

 if(result){
    alert('success message');
    success.style.display = "block";
    error.style.display = "none";
 }else{
    success.style.display = "none";
    error.style.display = "block";
 }

}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM