1

I want to remove null values from this array.

Array(
    [0] => Array( [fcmToken] => 123 )
    [1] => Array( [fcmToken] => )
    [2] => Array( [fcmToken] => 789 )
)

Expected Results

Array(
    [0] => Array( [fcmToken] => 123 )
    [1] => Array( [fcmToken] => 789 )
)
2
  • 1
    123 and 789 ?? where it come from? Commented Apr 29, 2017 at 9:41
  • You can do it using print_r(array_values(array_filter($entry))); Commented Apr 29, 2017 at 9:41

7 Answers 7

3

Here we are using foreach to iterate over values and using $value for non-empty $value["fcmToken"]

$result=array();
foreach($array as $key => $value)
{
    if(!empty($value["fcmToken"]))
    {
        $result[]=$value;
    }
}

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [fcmToken] => dfqVhqdqhpk
        )

    [1] => Array
        (
            [fcmToken] => dfgdfhqdqhpk
        )

)
Sign up to request clarification or add additional context in comments.

Comments

3

Use array_filter with a callback:

$r = array_filter($array, function($v) { return !empty($v['fcmToken']); });

For checking exactly null:

$r = array_filter($array, function($v) { return !is_null($v['fcmToken']); });

Comments

1

The best and easy single line solution for filter multidimensional array like below.

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

I hope this solution is work for you. for more detail please visit below link.

PHP: remove empty array strings in multidimensional array

1 Comment

Rather than doing an exact code copy-paste from another answer on SO, you should just flag/vote to close the question as a duplicate.
1

Danger! Do not trust empty() when dealing with numbers (or number-ish) values that could be zero!!! The same is true with using array_filter without a specific filtering parameter (as several answers on this page are using).

Look at how you can get the wrong output:

Bad Method:

$array = array(
    array("fcmToken" => '0'),
    array("fcmToken" => 123),
    array("fcmToken" => ''),
    array("fcmToken" => 789),
    array("fcmToken" => 0)
);

$result=array();                         // note, this line is unnecessary
foreach($array as $key => $value){       // note, $key is unnecessary
    if(!empty($value["fcmToken"])){      // this is not a reliable method
        $result[]=$value;
    }
}
var_export($result);

Output:

array (
  0 => 
  array (
    'fcmToken' => 123,
  ),
  1 => 
  array (
    'fcmToken' => 789,
  ),
)

The zeros got swallowed up!


This is how it should be done:

Instead, you should use strlen() to check the values:

Method #1: foreach()

foreach($array as $sub){
    if(strlen($sub["fcmToken"])){
        $result[]=$sub;
    }
}
var_export($result);

Method #2: array_filter() w/ anonymous function and array_values() for consistency

var_export(array_values(array_filter($array,function($a){return strlen($a['fcmToken']);})));

Output for either method:

array (
  0 => 
  array (
    'fcmToken' => '0',
  ),
  1 => 
  array (
    'fcmToken' => 123,
  ),
  2 => 
  array (
    'fcmToken' => 789,
  ),
  3 => 
  array (
    'fcmToken' => 0,
  ),
)

Demonstration of bad method and two good methods.

Comments

0

You need to do it using array_map, array_filter and array_values. Check below code :

$entry = array(
    array("fcmToken" => 'dfqVhqdqhpk'),
    array("fcmToken" => ''),
    array("fcmToken" => 'dfgdfhqdqhpk'),
);
echo "<pre>";
$entry = array_values(array_filter(array_map('array_filter', $entry)));
print_r($entry);

Comments

0

Use this recursive function which iterates over any subarray and remove all elements with a null-value.

function removeNullValues ($array) {
    foreach ($array as $key => $entry) {
        if (is_array($entry)) {
            $array[$key] = removeNullValues($entry);
            if ($array[$key] === []) {
                unset($array[$key]);
            }
        } else {
            if ($entry === null) {
                unset($array[$key]);
            }
        }
    }

    return $array;
}

Comments

-1

Try using array_map() to apply the filter to every array in the array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.