简体   繁体   中英

PHP remove array if subarray empty

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

这是我的数组

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();
<?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'])))

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;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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