Click to See Complete Forum and Search --> : [RESOLVED] Removing blank keys in array
LingoOutsider
May 29th, 2009, 09:38 AM
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...
function myfunction($v){
if ($v==="") {
return false; }
return true;}
array_filter($arrayday,"myfunction");
???
nmadd
May 29th, 2009, 01:16 PM
How about array_diff?
http://us3.php.net/array_diff
kows
May 29th, 2009, 02:35 PM
..? 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
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
?>
The above example will output:
Array
(
[0] => foo
[2] => -1
)
keys will be preserved, though.
nmadd
May 29th, 2009, 03:47 PM
..? 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(""));
kows
May 29th, 2009, 06:15 PM
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.
LingoOutsider
Jun 1st, 2009, 01:35 AM
Thanks I'll give them both a try :)
LingoOutsider
Jun 1st, 2009, 03:17 AM
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?
LingoOutsider
Jun 1st, 2009, 04:42 AM
never mind, I can do it with...
foreach ($array as $value){
echo $value;}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.