dongshao5573
2014-10-02 17:39
浏览 79
已采纳

如何使用PHP从数组中排除空键?

When doing a query in data bank I can receive both arrays as result:

Array
(
    [] => 401
    [BETA] => 223
    [GAMMA] => 195
    [DELTA] => 189
)

Array
(
    [ALFA] => 294
    [BETA] => 223
    [GAMMA] => 195
    [DELTA] => 189
)

What can I do to exclude the entirely line when the key is empty?

I tried something like this, but without success:

foreach($array as $key => $value){
    if(array_key_exists($key, $value) && is_null($value[$key])) {
        unset($key);
    }
    $array = $value;
}

Many thanks in advance!

图片转代码服务由CSDN问答提供 功能建议

在数据库中进行查询时,我可以接收两个数组: < pre> Array ( [] =&gt; 401 [BETA] =&gt; 223 [GAMMA] =&gt; 195 Δ=&gt; 189 ) \ nArray ( [ALFA] =&gt; 294 [BETA] =&gt; 223 [GAMMA] =&gt; 195 Δ=&gt; 189 ) < / pre>

当密钥为空时,如何排除整行?

我尝试了类似的东西,但没有成功:

  foreach($ array as $ key =&gt; $ value){
 if(array_key_exists($ key,$ value)&amp;&amp;&amp; is_null($ value [$ key]))  {
 unset($ key); 
} 
 $ array = $ value; 
} 
   
 
 

非常感谢提前! \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • du131642 2014-10-02 18:10
    已采纳

    Since the key is an empty string you could either remove that specific key:

    $test = array(''=>'test', 'test'=>'test');
    
    unset($test['']);
    
    print_r($test);
    

    Or remove all keys that contain only whitespace:

    function array_remove_blank_keys($array) {
    
        foreach ($array as $key => $value) {
    
            if (preg_match('/^\s*$/', $key)) {
                unset($array[$key]);
            }
    
        }
    
        return $array;
    
    }
    
    $test = array(''=>'test', 'test'=>'test');
    
    print_r($test);
    print_r(array_remove_blank_keys($test));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongleiwei2182 2014-10-02 17:44

    Keys are never empty. They would have a numeric index. So just check to see if the key is numeric then unset.

    foreach($array as $key => $value){
        if(is_numeric($key)) {
            unset($key);
        }
    
    }
    

    and you don't need to do $array = $value;

    评论
    解决 无用
    打赏 举报
  • doujiu7680 2014-10-02 17:52

    The key is not null, it is an empty string. eg:

    $foo = array(""=>"bar");
    print_r($foo);
    

    Output:

    Array
    (
        [] => bar
    )
    

    So you would want either empty($key) or $key == '' as your test, the difference being that empty() can match quite a few things.

    评论
    解决 无用
    打赏 举报
  • douzhen5158 2014-10-02 18:07

    Use the empty($var) function for evaluation. It considers these values empty and returns true if provided with one:

    null: the null value,

    '': empty string,

    0: numeric zero. Only if it is not assigned by direct or indirect assignment such as $var=0 or $var=1-1. Note that the zero string ('0') is not empty,

    array(): an empty array,

    $var;: an unassigned variable,

    false: value of false,

    ' ': just a space string.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题