简体   繁体   English

如何在PHP中删除包含在关联数组中的数组内的所有空值?

[英]How to remove all null values inside an array, which is enclosed within an associate array, in PHP?

My question may seem fairly simple, but I have tried multiple techniques, and none seem to yield a proper answer. 我的问题似乎很简单,但是我尝试了多种技术,但似乎都没有一个合适的答案。

I have an associative array as follows: 我有一个关联数组,如下所示:

$array = array("TB1_course" => array(null, 'CHEM 2E03', null, "BIO 1A03"),  
          "TB1_section" => array(null, 'CHEM 2E03', null, "BIO 1A03"), 
          "TB1_session" => array(null, 'CHEM 2E03', null, "BIO 1A03")
          );

Now I would like to remove all the null elements in my arrays, for the respective associative array. 现在,我想删除数组中所有关联数组的空元素。

My attempt was as follows: 我的尝试如下:

foreach($array as $key=>$value){
    for($i=0; $i<sizeof($value);$i++){
        if ($value[$i]==null){
           unset($value[$i]); 
        }
        $array[$key]=$value;
    }
}

print_r($array);

But my output is also retianing the indexes of the array. 但是我的输出也重新排列了数组的索引。 My output is as follows: 我的输出如下:

Array
(
[TB1_course] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

[TB1_section] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

[TB1_session] => Array
    (
        [1] => CHEM 2E03
        [3] => BIO 1A03
    )

)

I would like to remove the indexes, so that there are only two elements inside of my arrays. 我想删除索引,以便数组中只有两个元素。 "CHEM 2E03" should be the 0th index, and "BIO 1A03" should be the 1st index. “ CHEM 2E03”应为第一个索引,而“ BIO 1A03”应为第一个索引。 I am using PHP 5.4. 我正在使用PHP 5.4。

The array_values() function retains the value and resets the indexed of the array. array_values()函数保留该值并重置数组的索引。 Below is it's implementation for your purpose: 以下是针对您的目的的实现:

foreach($array as $key=>$value){
  for($i=0; $i<sizeof($value);$i++){
    if ($value[$i]==null){
       unset($value[$i]); 
    }
    $array[$key] = array_values($value);
  }
}

您可以使用array_values()函数重新索引数组。

Using unset() removes the value but keeps the key as it is. 使用unset()会删除该值,但会保持原样。 The best solution here will be to use array_splice() which will remove the element completely. 最好的解决方案是使用array_splice() ,它将完全删除该元素。 For your code, it should be 对于您的代码,应该是

array_splice($value, $i, 1);

instead of 代替

unset($value[$i]); 

You can use a function then this will recursively filter and reset your indexe using array_filter and array_values 您可以使用一个function然后它将使用array_filterarray_values递归过滤并重置索引

function array_filter_recursive($input) 
{ 
    foreach ($input as &$value) 
    { 
        if (is_array($value)) 
        { 
            $value = array_filter_recursive($value); 
        } 
    } 
    return array_values(array_filter($input)); 
} 

$array = [
    "TB1_course" => array(null, 'CHEM 2E03', null, "BIO 1A03"),  
    "TB1_section" => array(null, 'CHEM 2E03', null, "BIO 1A03"), 
    "TB1_session" => array(null, 'CHEM 2E03', null, "BIO 1A03")
];

print_r(array_filter_recursive($array));

Here is a phpfiddle to run the above: http://phpfiddle.org/main/code/cgbj-h10z 这是运行上述命令的phpfiddle: http ://phpfiddle.org/main/code/cgbj-h10z

Resources 资源资源
1. http://php.net/manual/en/function.array-filter.php 1. http://php.net/manual/zh/function.array-filter.php
2. http://php.net/manual/en/function.array-values.php 2. http://php.net/manual/en/function.array-values.php

try 尝试

foreach($array as $key=>$value){
    for($i=0; $i<sizeof($value);$i++){
        if ($value[$i]==null){
           unset($value[$i]); 
        }
        $array[$key]=$value;
    }
}

$arr = array_map('array_values', $array);
print_r($arr);

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

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