[RESOLVED] Removing blank keys in array
After using array_unique() I appear to have an array that has blank fields in it.
(1-1,1-5,,1-0,,,,1-2)
Any Sugestions on the removal of said fields, I've tried this...
PHP Code:
function myfunction($v){
if ($v==="") {
return false; }
return true;}
array_filter($arrayday,"myfunction");
???
Re: Removing blank keys in array
Re: Removing blank keys in array
..? how would array_diff() do anything? that function compares multiple arrays. he only has one array.
you should be able to use array_filter() without a callback function to remove false, empty and null values. there is an example on php.net:
PHP Code:
<?php
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
?>
The above example will output:
Code:
Array
(
[0] => foo
[2] => -1
)
keys will be preserved, though.
Re: Removing blank keys in array
Quote:
Originally Posted by
kows
..? how would array_diff() do anything? that function compares multiple arrays. he only has one array.
I don't know. Does this work?
$arr = array_diff($arr, array(""));
Re: Removing blank keys in array
touche! I didn't even think about that. array_filter() with no callback and array_diff() with an array with an empty string should both produce the same result. though, array_filter() seems like it could be faster.
Re: Removing blank keys in array
Thanks I'll give them both a try :)
Re: Removing blank keys in array
Because the $keys are no longer [1][2][3]... and have gaps in them [1][3][6]... how can I echo/print them in a list?
Re: Removing blank keys in array
never mind, I can do it with...
PHP Code:
foreach ($array as $value){
echo $value;}