如何在具有任何值空白多维数组的情况下从数组中删除主索引

时间:2017-03-01 16:58:25

标签: php arrays multidimensional-array

如何删除主数组索引

离。

原始数组

[4] => Array
    (
        [fullname] => chaman pura
        [email] => chamana@gmail.com
    )

[5] => Array
    (
        [fullname] => sagar one
        [email] => 
    )

[6] => Array
    (
        [fullname] => hello how
        [email] => how@gmail.com
    )

输出应为:

[4] => Array
        (
            [fullname] => chaman pura
            [email] => chamana@gmail.com
        )

    [6] => Array
        (
            [fullname] => hello how
            [email] => how@gmail.com
        )

试过:

$postArr = array_map('array_filter', $postArr);
$postArr = array_filter( $postArr );

及其给予

[5] => Array
        (
            [fullname] => sagar one
        )

它唯一删除子键,而不是删除父索引

2 个答案:

答案 0 :(得分:2)

您只需使用array_filter

$arr = [ '4' => [ 'fullname' => 'chaman pura', 'email' => 'chamana@gmail.com' ],
         '5' => [ 'fullname' => 'sagar one', 'email' => '' ],
         '6' => [ 'fullname' => 'hello how', 'email' => 'how@gmail.com' ] ];


var_dump(array_filter($arr, function ($i) { return !empty($i['email']); }));

答案 1 :(得分:2)

您需要尽可能地循环数组并检查内部数组电子邮件。

$postArr = [
    ["fullname" => "chaman pura", "email" => "chamana@gmail.com"],
    ["fullname" => "sagar one", "email" => ""],
    ["fullname" => "hello how", "email" => "how@gmail.com"]
];

$postArrLen = count($postArr);
for($i=0; $i < $postArrLen; $i++){
    if(empty($postArr[$i]['email'])){
        unset($postArr[$i]);
    }
}

print_r($postArr);

其他

我想这可能只是一个foreach

foreach($postArr as $arr){
    if(empty($arr['email'])){
        unset($arr);
    }
}