|
-
May 29th, 2009, 09:38 AM
#1
Thread Starter
Addicted Member
[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");
???
-
May 29th, 2009, 01:16 PM
#2
Re: Removing blank keys in array
-
May 29th, 2009, 02:35 PM
#3
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.
-
May 29th, 2009, 03:47 PM
#4
Re: Removing blank keys in array
 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(""));
-
May 29th, 2009, 06:15 PM
#5
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.
-
Jun 1st, 2009, 01:35 AM
#6
Thread Starter
Addicted Member
Re: Removing blank keys in array
Thanks I'll give them both a try
-
Jun 1st, 2009, 03:17 AM
#7
Thread Starter
Addicted Member
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?
-
Jun 1st, 2009, 04:42 AM
#8
Thread Starter
Addicted Member
Re: Removing blank keys in array
never mind, I can do it with...
PHP Code:
foreach ($array as $value){
echo $value;}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|