PHP 数组修剪空白索引值

发布于 2024-08-24 06:04:46 字数 312 浏览 3 评论 0原文

如何修剪 PHP 数组并删除所有空索引

Array
(
    [0] => 
    [1] =>
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] => 4
    [8] => 6
    [9] =>

)

输出应该类似于

 Array
    (
        [0] => 4
        [1] => 6   
    )

How to trim a PHP array and remove all empty indexes

Array
(
    [0] => 
    [1] =>
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] => 4
    [8] => 6
    [9] =>

)

Output should be like

 Array
    (
        [0] => 4
        [1] => 6   
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-08-31 06:04:46

您正在寻找 array_filter 函数 ;-)

例如,这部分代码:

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr);
var_dump($arr_filtered);

将为您提供以下输出:

array
  7 => int 4
  8 => int 6

请注意,所有“假”值已被删除。

如果你想更具体,你可以指定你自己的过滤功能。例如,要从数组中仅删除 null,我可以使用这个:

function my_filter($item) {
    if ($item === null) {
        return false;
    }
    return true;
}

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr, 'my_filter');
var_dump($arr_filtered);

我会得到:

array
  1 => int 0
  3 => int 0
  4 => string '' (length=0)
  6 => string '' (length=0)
  7 => int 4
  8 => int 6
  9 => string '' (length=0)

You are looking for the array_filter function ;-)

For instance, this portion of code :

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr);
var_dump($arr_filtered);

Will give you the following output :

array
  7 => int 4
  8 => int 6

Note that all "falsy" values have been removed.

And if you want to be more specific, you can specify your own filtering function. For instance, to remove only nulls from the array, I could use this :

function my_filter($item) {
    if ($item === null) {
        return false;
    }
    return true;
}

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr, 'my_filter');
var_dump($arr_filtered);

And I'd get :

array
  1 => int 0
  3 => int 0
  4 => string '' (length=0)
  6 => string '' (length=0)
  7 => int 4
  8 => int 6
  9 => string '' (length=0)
濫情▎り 2024-08-31 06:04:46

另一种方式:

<?php

$array = array(
    0 => 0,
    1 => ,
    2 => '',
    3 => 4,
    4 => 6,
    5 => null
);

foreach( $array as $a )
{
    if( !empty($a) AND $a != NULL AND $a != 0 ) // NULL, 0
    {
        $new_array[] = $a;
    }
}

print_r( $new_array );

?>

输出将是:

Array
(
    [0] => 4
    [1] => 6
)

Here another way:

<?php

$array = array(
    0 => 0,
    1 => ,
    2 => '',
    3 => 4,
    4 => 6,
    5 => null
);

foreach( $array as $a )
{
    if( !empty($a) AND $a != NULL AND $a != 0 ) // NULL, 0
    {
        $new_array[] = $a;
    }
}

print_r( $new_array );

?>

Output will be:

Array
(
    [0] => 4
    [1] => 6
)
誰認得朕 2024-08-31 06:04:46

对我来说听起来像是家庭作业。

我建议你看一下 array_filter 函数。这似乎是最合适的选择。

Sounds like homework to me.

I'd suggest you take a look at the array_filter function. That seems to be the most appropriate option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文