PHP remove array if subarray empty

做~自己de王妃 提交于 2021-01-29 15:54:19

问题


my array image just like this, if subarray "name" is empty or null i want delete array, how to do that ?

here's my array

here my current script

    $data       = array();
    $fixedData  = array();
    $countyName = array();
    $numrow = 2;
    echo "<pre>";
    // insert to tb participant => 1
    foreach($sheet as $key => $row){
    $data[] = array(
            'name' => $this->split_name($row['B']),
            'phone' => $row['D'],
            'mobile' => $row['E'],
            'institution' => $row['F'],
            'departement' => $row['G'],
            'address' => $row['H'],
            'country' => $row['I'],
    );

      $numrow++; 
    }
    unset($data[0]); //delete first row
    $data = array_values($data);

    //loop search data 

    var_dump ($data);
    die();

回答1:


Assume that you have the following data set,

$array = [
    [
        'name' => 'not null', 'phone' => 12546
    ],[
        'name' => '', 'phone' => 852147
    ],[
        'name' => null, 'phone' => 96325874
    ],[
        'name' => 'have value', 'phone' => 12546
    ],
];

You can filter the nulled or empty values like several ways :

1-

foreach ($array as $key => &$value) {
    if (empty($value['name']) || is_null($value['name'])) {
        $value = null;
    }
}

$array = array_filter($array);

2-

$newData = [];
foreach ($array as $key => $value) {
    if (!empty($value['name']) && !is_null($value['name'])) {
        $newData[] = $value;
    }
}

3- using array_walk

$newData = [];
array_walk($array, function ($value, $key) use (&$newData) {
    if (!empty($value['name']) && !is_null($value['name'])) {
        $newData[] = $value;
    }
});

4- using array_filter

$newData = array_filter($array, function ($value) {
    if (!empty($value['name']) && !is_null($value['name'])) {
        return $value;
    }
});



回答2:


<?php

$data       = array();
$fixedData  = array();
$countyName = array();
$numrow = 2;
echo "<pre>";
// insert to tb participant => 1
foreach($sheet as $key => $row){

    if($this->split_name($row['B'])!=='' && $this->split_name($row['B'])!==NULL){
        $data[] = array(
            'name' => $this->split_name($row['B']),
            'phone' => $row['D'],
            'mobile' => $row['E'],
            'institution' => $row['F'],
            'departement' => $row['G'],
            'address' => $row['H'],
            'country' => $row['I'],
        );
        $numrow++;
    }

}



//loop search data

var_dump ($data);
die();

I simple put an if condition inside your loop so you can check if your value is null or empty and if it is then you don't fill your new array. Also moved your counter inside the if so you increment it only in a success array push

A more "elegant" way for your if condition is this as well:

if (!empty($this->split_name($row['B'])) && !is_null($this->split_name($row['B'])))


来源:https://stackoverflow.com/questions/52091595/php-remove-array-if-subarray-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!