简体   繁体   English

检查所有数组是否有值且不为空

[英]Check all the array has value and not empty

index.html 的index.html

<form id="players_form"  >
    <input type=" text" name="main_name[]" value="1">
    <input type=" text" name="main_name[]" value="2">
    <input type=" text" name="main_name[]" value="3">
    <input type=" text" name="main_name[]" value="4">
    <input type=" text" name="main_name[]" value="5">

    <input type=" text" name="sub_name[]" value="6">
    <input type=" text" name="sub_name[]" value="7">
    <input type=" text" name="sub_name[]" value="8">
    <input type=" text" name="sub_name[]" value="9">
    <input type=" text" name="sub_name[]" value="">
    <input type="button" value="Submit" id="submit">

 </form>

script 脚本

$('#submit').click(function() {
var data = JSON.stringify($("#players_form").serializeArray());
// alert(data);
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type: 'POST',
data: {
  list: data
},
url: 'process.php',
success: function(responseText) {
  if (responseText == 1) {
    alert("Sucess");
  }

process.php process.php

$decoded = json_decode($_REQUEST['list'], true);
print_r(array_filter($decoded));

Result 结果

Array( [0] => Array ( [name] => main_name[] [value] => 1 )
[1] => Array ( [name] => main_name[] [value] => 2 ) 
[2] => Array ( [name] => main_name[] [value] => 3 ) 
[3] => Array ( [name] => main_name[] [value] => 4 ) 
[4] => Array ( [name] => main_name[] [value] => 5 ) 
[5] => Array ( [name] => sub_name[] [value] => 6 ) 
[6] => Array ( [name] => sub_name[] [value] => 7 ) 
[7] => Array ( [name] => sub_name[] [value] => 8 ) 
[8] => Array ( [name] => sub_name[] [value] => 9 ) 
[9] => Array ( [name] => sub_name[] [value] => ))

Expected code 预期代码

if(all the arrays has value)
{
 // True
}
else
{
 Any one of the values in array is empty 
 }

Hi friends in the above code I'm sending the form values to process.php through AJAX function , in process.PHP I should check wheather all the text box should contain values then it should proceed to next step else , it should echo sorry you have empty values . 在上面的代码喜的朋友我送表单值通过对process.php AJAX function ,在process.PHP我应该检查wheather的所有文本框应包含的值,那么它应该继续到别的下一步,应该呼应对不起你空值。 In my code I have last box value as empty <input type=" text" name="sub_name[]" value=""> in result also it returns [9] => Array ( [name] => sub_name[] **[value] =>** )) , How to check whether all the array has value or not . 在我的代码中,我的最后一个框值为空<input type=" text" name="sub_name[]" value="">结果也returns [9] => Array ( [name] => sub_name[] **[value] =>** )) ,如何检查所有数组是否都具有value。 Thanks in advance 提前致谢

The best way use empty() function 最好的方法使用empty()函数

if(!empty($decoded))
{
 // True
}
else
{
 Any one of the values in array is empty 
 }

First, you need to validate data before sending it to server. 首先,您需要先验证数据,然后再将其发送到服务器。 For example: 例如:

var dataValid = true;
$("#players_form input").each(function(){
    if ($(this).val() == ''){
        dataValid = false;
    }
});

if (dataValid){
   // send data
} else {
   alert('Fill in all the data!');
}

Then on server, same thing: 然后在服务器上,执行以下操作:

$valid = true;
foreach($decoded as $input){
    if ($input['value'] == ''){
        $valid = false;
    }
}

if ($valid){
    // done good
} else {
    echo "Fill in all the data!";
}

It's better to check before POSTing the data, [].every can do that. 最好在发布数据之前进行检查, [].every都可以这样做。 every allows you to check if all elements of an array pass a certain test. every允许您检查数组的所有元素是否通过特定测试。

var ok = data.every(function(element) {
    return element.value; // if value is empty, it's false
});

if (ok) {
    // POST request
} else {

}

In PHP5 (>=5.5.0) you can do search through a multi dimensional array 在PHP5(> = 5.5.0)中,您可以搜索多维数组

$user=Array
(
(0) => Array
    (
        (name) => 'Susan',
        (age) => 21
    ),

(1) => Array
    (
        (name) => 'Mark',
        (age) => 33
    ),

(2) => Array
    (
        (name) => 'Michael',
        (age) => 25
    ));
$key = array_search(21, array_column($user, 'age'));

You can search '' value in your array. 您可以在数组中搜索''值。

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

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