-1

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();
3
  • Have you tried empty($data[0]['name']) Commented Aug 30, 2018 at 7:41
  • here just deleted name subarray , not the parent array Commented Aug 30, 2018 at 7:45
  • do you have an already created array? or you want to avoid adding the null values in the creating phase? Commented Aug 30, 2018 at 8:00

2 Answers 2

0

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;
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

here my array sir, postimg.cc/image/kstaxmhy3, i want if array named as "name" if "" or null the parent array is removed ... how to do that ?
have you tried any of my posted solutions? that what is already happens
not working sir :( , pastebin.com/raw/GYDzGvfC it's still show 645 arrays , it's should be just 381 arrays ...
can you send a json encoded array? I can not reproduce the dumped array at all
0
<?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'])))

4 Comments

not working sir, can u check my full script on pastebin ? here it's pastebin.com/wULeq4hJ
Could you elaborate more on what's not working? Your $data array should have only the nested array which have a value in the name field.
postimg.cc/image/kstaxmhy3 if array name empty like this i want remove them sir, but it's still displaying
print_r your array and provide us with a sample of your data to reproduce the problem. Not in an image format please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.