php Foreach一个数组来检查一个键的值是否不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:34:07  来源:igfitidea点击:

Foreach an array to check if the value of a key is not empty

phparrays

提问by dikidera

I have an array that goes like this

我有一个像这样的数组

$array = array('key1' => $_GET['value1'], 'key2' => $_GET['value2']);

I want to check if the key's value is not empty. Say a user goes to the webpage and requests this http://mysite.com/page.php?value1=somevalue&value2=which means that value2 is empty or say that it is missing from the query string i want to be able to check if it's there and is not empty.

我想检查键的值是否不为空。假设用户访问网页并请求此 http://mysite.com/page.php?value1=somevalue&value2=这意味着 value2 为空或说它从查询字符串中丢失我希望能够检查它在那里并且不是空的。

I tried this

我试过这个

foreach($array as $key => $value)
{
    if(empty($value))
    {
        //spout some error
    }
}

but even though i enter this http://mysite.com/page.php?value1=somevaluemeaning that value2 is missing, i don't get the error.

但即使我输入此http://mysite.com/page.php?value1=somevalue意味着缺少 value2,我也没有收到错误消息。

How do i achieve what i want?

我如何实现我想要的?

采纳答案by Jared Farrish

When I do this:

当我这样做时:

<?php 

$array = array('key1' => $_GET['value1'], 'key2' => $_GET['value2']);

foreach($array as $key => $value)
{
    if(empty($value))
    {
        echo "$key empty<br/>";
    }
    else
    {
        echo "$key not empty<br/>";
    }
}

?>

With:

和:

http://jfcoder.com/test/arrayempty.php?value1=somevalue

http://jfcoder.com/test/arrayempty.php?value1=somevalue

EDIT: And this - http://jfcoder.com/test/arrayempty.php?value1=somevalue&value2=

编辑:这 - http://jfcoder.com/test/arrayempty.php?value1=somevalue&value2=

I get:

我得到:

key1 not empty
key2 empty

I'm thinking you need to give more context to your question about actual use, since there is probably some other detail throwing it off.

我认为您需要为有关实际使用的问题提供更多背景信息,因为可能还有其他一些细节将其抛弃。

EDIT

编辑

This doesn't make any sense, but this was provided in a comment:

这没有任何意义,但这是在评论中提供的:

$array = array(
    'key1' => '".mysql_real_escape_string($_GET['value1'])."',
    'key2' => '".mysql_real_escape_string($_GET['value2'])."'
);

This actually doesn't parse due to the single quotes being mixed in with the single quoted string, and the fact that this, even if it did parse, would set the array piece equal to the literal string, not the function result.

这实际上不会解析,因为单引号与单引号字符串混合在一起,事实上即使它确实解析了,也会将数组片段设置为等于文字字符串,而不是函数结果。

I gather that maybe it was:

我猜想可能是:

$array = array(
    'key1' => "'".mysql_real_escape_string($_GET['value1'])."'",
    'key2' => "'".mysql_real_escape_string($_GET['value2'])."'"
);

Which would indicate that a string would always be returned that was at least '', which would never be empty, due to the single quotes.

这表明将始终返回至少为 的字符串'',由于单引号,该字符串永远不会为空。

Consider PDO.

考虑PDO

回答by Chris McClellan

OH THIS WILL DO IT! this will add the value key pair to a new array.

哦,这样就行了!这会将值键对添加到新数组中。

if (isset($_GET)) {
  $get_keys = array_keys($_GET);
  foreach ($array_keys as $k) {
    if ($_GET[$k] != '') {
      $new_array[$k] = $_GET[$k];
    }
  }
}

回答by Chris McClellan

I would check to see if the global $_GET var is set then loop through the values.

我会检查是否设置了全局 $_GET 变量,然后循环遍历这些值。

EDIT: PUSH GET VARS ONTO ARRAY IF THEY ARE SET (this will push the values onto an array and assign it value1, value2, etc.

编辑:PUSH GET VARS ONTO ARRAY IF THE Y SET

if (isset($_GET)) {
  foreach ($_GET as $get) {
    if ($get != '') {
      $new_array['value'.$i] = $get;
      ++$i;
    }
  }
}

回答by Skrol29

Your foreach() loop is scanning only given values, not expected values. You need to define expected values and do a loop like this:

您的 foreach() 循环仅扫描给定值,而不是预期值。您需要定义预期值并执行如下循环:

foreach($expected_keys as $key) {
    if ( (!isset($array[$key])) || empty($array[$key]) ) {
        //spout some error
    }
}

回答by Jase

empty() also performs an isset().

empty() 也执行isset()。

So if the key is missing, empty() won't cause an error. It validate as TRUE because it is empty

因此,如果缺少密钥,empty() 不会导致错误。它验证为 TRUE 因为它是空的

you would need to do a seperate isset() check to make sure the key is there

您需要进行单独的 isset() 检查以确保密钥在那里