sohrab_ark Posted December 23, 2008 Share Posted December 23, 2008 Hi I'm newbie in php. I have an array that contain some blank data.I want to remove these blank elements. I tried to do this with array_slice, but I completely confused ??? also I should say my array every time will change. any help is my appreciate sohrab Quote Link to comment Share on other sites More sharing options...
Jabop Posted December 23, 2008 Share Posted December 23, 2008 <?php foreach($array as $key=>$value) { if ($value=='') { unset($array[$key]); } } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted December 23, 2008 Share Posted December 23, 2008 Also, if you want to reassign the indexes to their new values, perform this afterwards: <?php $array=array_keys($array); ?> EDIT: Just did some googling and found a more simple function. http://us2.php.net/array_filter Quote Link to comment Share on other sites More sharing options...
sohrab_ark Posted December 23, 2008 Author Share Posted December 23, 2008 <?php foreach($array as $key=>$value) { if ($value=='') { unset($array[$key]); } } ?> thanks for your help but it didn't work <?php $input = array("a", "b", "c", "d", "e"); foreach($array as $key=>$value) { if ($value=='d') { unset($input[$key]); } } $input=array_keys($input); for($i0=0;$i0<count($input);$i0++) { echo $input[$i0]."<br/>"; // result is 0 1 2 3 4 } ?> I want result be a b c e Quote Link to comment Share on other sites More sharing options...
Jabop Posted December 23, 2008 Share Posted December 23, 2008 foreach $input... Quote Link to comment Share on other sites More sharing options...
sohrab_ark Posted December 23, 2008 Author Share Posted December 23, 2008 thanks about $input.... but it doesn't work yet. <?php $input = array("a", "b", "c", "d", "e"); foreach($input as $key=>$value) { if ($value=='d') { unset($input[$key]); } } $input=array_keys($input); for($i0=0;$i0<count($input);$i0++) { echo $input[$i0]." ";// result 0 1 2 4 } ?> result is 0 1 2 4 and without [i]$input=array_keys($input);[/i] result is a b c Quote Link to comment Share on other sites More sharing options...
sohrab_ark Posted December 23, 2008 Author Share Posted December 23, 2008 solved <?php $input = array("a", "b", "c", "d", "e"); foreach($input as $key=>$value) { if ($value=='d') { unset($input[$key]); } } $input=array_values($input); for($i0=0;$i0<count($input);$i0++) { echo $input[$i0]." ";// result 0 1 2 4 } ?> I should use array_values($input); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.