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 Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/ 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]); } } ?> Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722663 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 Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722668 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 Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722674 Share on other sites More sharing options...
Jabop Posted December 23, 2008 Share Posted December 23, 2008 foreach $input... Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722686 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 Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722699 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); Link to comment https://forums.phpfreaks.com/topic/138226-solved-array_slice-removing-element-in-array/#findComment-722715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.