cemeteryridge Posted April 14, 2010 Share Posted April 14, 2010 Is there a way to rearrange an array so there are no blank indexes? $tank[0] = "User1"; $tank[1] = ""; $tank[2] = ""; $tank[3] = "User2"; into this $tank[0] = "User1"; $tank[1] = "User2"; Is there a function of some sort? Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/ Share on other sites More sharing options...
teamatomic Posted April 14, 2010 Share Posted April 14, 2010 $new_array=array_filter($array) HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042027 Share on other sites More sharing options...
cemeteryridge Posted April 15, 2010 Author Share Posted April 15, 2010 Ive tried that, but it doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042092 Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 $newarr = array_reverse($tank); foreach ($newarr as $key => $val) { if (empty($val)) unset($tank[$key]); } Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042102 Share on other sites More sharing options...
jcbones Posted April 15, 2010 Share Posted April 15, 2010 Ive tried that, but it doesn't seem to work. Funny, I just tried it, and it did. <?php $tank[0] = "User1"; $tank[1] = ""; $tank[2] = ""; $tank[3] = "User2"; echo '<pre>'; print_r(array_filter($tank)); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042106 Share on other sites More sharing options...
cemeteryridge Posted April 15, 2010 Author Share Posted April 15, 2010 That not how I need it to work. I need the empty values of index 1,2 to be erased and the value of 3 be relocated to 1; like I explained in my first post; $tank[0] = "User1"; $tank[1] = ""; $tank[2] = ""; $tank[3] = "User2"; to $tank[0] = "User1"; $tank[1] = "User2"; Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042108 Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 Hmm.. thought mine would do that. Guess I forgot that part about unset. Anyways.. $tank = array_values(array_filter($tank)); var_dump($tank); Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042114 Share on other sites More sharing options...
jcbones Posted April 15, 2010 Share Posted April 15, 2010 Sorry, didn't notice the arrangement of the keys in OP. <?php $tank[0] = "User1"; $tank[1] = ""; $tank[2] = ""; $tank[3] = "User2"; $tank = array_filter($tank); sort($tank); echo '<pre>'; print_r($tank); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042120 Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 Sorry, didn't notice the arrangement of the keys in OP. <?php $tank[0] = "User1"; $tank[1] = ""; $tank[2] = ""; $tank[3] = "User2"; $tank = array_filter($tank); sort($tank); echo '<pre>'; print_r($tank); echo '</pre>'; ?> Maybe not with those values, but wouldn't sort have more of an effect than the OP would want? Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042124 Share on other sites More sharing options...
jcbones Posted April 15, 2010 Share Posted April 15, 2010 It may, although it will sort alphabetically, besides I like giving people more for their money. Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042172 Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 It may, although it will sort alphabetically, besides I like giving people more for their money. Unless sorting will alter the way he wants the data, then you won't get any money because you didn't perform the task as specified. Quote Link to comment https://forums.phpfreaks.com/topic/198567-rearanging-an-array/#findComment-1042188 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.