I have an array:
$array = array("1" => "", "2" => "" , "3" => "data1", "4" => "" , "5" => "data2", "6" => "" );
It's declared with for
leaving some empty values.Array can be too long. I want a method to find filled array numbers in PHP.
Check this, will give you all empty array keys
<?php
$array = array("1" => "", "2" => "" , "3" => "data1", "4" => "" , "5" => "data2", "6" => "" );
$new = array_filter($array, 'strlen');
$result = array_diff($array, $new);
print_r($result);
?>
If you want to remove the empty value keys use array_filter
<?php
$array = array("1" => "", "2" => "" , "3" => "data1", "4" => "" , "5" => "data2", "6" => "" );
$new = array_filter($array, 'strlen');
print_r($new);
?>
Something like this?
$newArray = array();
foreach($array as $key=> $value)
{
if($value != '')
$newArray[$key] = $value;
}
print_r($newArray);
I'm not 100% sure about what you want, but I think this might help you. Let me know if this isn't what you wanted.
foreach ($array as $v) {
if ($v) {
// this value is not empty
}
}
$array = array("1" => "", "2" => "" , "3" => "data1", "4" => "" , "5" => "data2", "6" => "" );
$array_with_keys = array();
foreach($array as $key=> $value)
{
if($value != '')
$array_with_keys[] = $key;
}
print_r($array_with_keys);
Since you want only keys this would return you an array with keys which you can use to update the database using a foreach loop