Reputation:
Say I have an array that looks like this:
Array
(
[0] =>
[1] => 2017-01-01 00:00:00
)
How can I dynamically check to see if the area has any empty values?
Upvotes: 0
Views: 3128
Reputation: 51
For this you have more possibility:
You can use array_filter
function without without a second parameter
array_filter([ 'empty' => null, 'test' => 'test']);
but for this be carful because this remove all values which are equal false (null, false, 0)
You can use array_filter
function with callback function:
function filterEmptyValue( $value ) {
return ! empty( $value );
}
array_filter([ 'empty' => null, 'test' => 'test'], 'filterEmptyValue');
You can use foreach or for:
$array = ['empty' => null, 'test' => 'test'];
foreach($array as $key => $value) {
if(empty($value)) {
unset($array[$key]);
}
}
$array = [null, 'test'];
for($i = 0; $i < count($array); $i++){
if(empty($array[$i])) {
unset($array[$i]);
}
}
This is samples so you must think and make good solution for your problem
Upvotes: 0
Reputation: 41810
You can see if it has any empty values by comparing the array values to the result of array_filter
(which removes empty values.)
$has_empty_values = $array != array_filter($array);
Upvotes: 1
Reputation: 725
Something like
// $array = [ ... ];
$count = count($array);
for( $i=0; $i<=$count; $i++){
if( $array[$count] == 0 ){
// Do something
}
}
Upvotes: 0
Reputation: 7911
You can use empty()
:
$array = [
null,
'2017-01-01 00:00:00',
'',
[],
# etc..
];
foreach($array as $key => $value){
if(empty($value)){
echo "$key is empty";
}
}
See the type comparison table for more information.
Upvotes: 1