thepip3r Posted January 4, 2010 Share Posted January 4, 2010 i have a script reading a CSV file into an array. sometimes (based on the software version generating the CSV), there are blank cells which result in blank array elements which make the processing off. i've tried to remove those blank elements using unset() which does work but it doesn't reorder the array: so where i'm expecting $array[20], in the CSV with blank entries, the value i'm looking for is at $array[21] with $array[20] being blank. using unset, i've been able to wipe out this blank entry but my question is this: is there an inherent SORT or other function that will reorder the array as well based off of the keys? i've looked at ksort() etc but they all seem to maintain index association. i am fine for creating my own, i'd just rather use the PHP version if there is one. TIA! Edit: let me try to make this a bit more clear: so in my ideal form, the array looks similar to: [20] => Monday - November 16, 2009 [21] => 11/16/2009 12:00 [22] => 3 my problem array looks like: [20] => Monday - November 16, 2009 [22] => 11/16/2009 12:00 [23] => 3 this most recent set is of data is after the unset of the blank field was done on [21]. with this last array, is there a sort function i can pass so that it will reorder the keys so they are in order again with [22] with the date then becomes [21] and so on... does that clear it up? Quote Link to comment https://forums.phpfreaks.com/topic/187181-reorder-an-arrays-keys-but-reassociate-their-index-as-well/ Share on other sites More sharing options...
mikesta707 Posted January 4, 2010 Share Posted January 4, 2010 The following worked for me $arr = array("d", "d", "d", "d", "d", "", "", "", "d", "d"); echo count($arr) . "<br />"; $new = array(); foreach($arr as $arr){ if (!empty($arr)){ $new[] = $arr; } } echo count($new) . "<br />"; print_r($new); ?> then $new would be the array without the blank values output: Array ( [0] => d [1] => d [2] => d [3] => d [4] => d [5] => [6] => [7] => [8] => d [9] => d ) 10 7 Array ( [0] => d [1] => d [2] => d [3] => d [4] => d [5] => d [6] => d ) the important part is the foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/187181-reorder-an-arrays-keys-but-reassociate-their-index-as-well/#findComment-988439 Share on other sites More sharing options...
thepip3r Posted January 4, 2010 Author Share Posted January 4, 2010 yeah i knew i could add the values to another array -- just thought i remembered a sort that would do this for me. thanx mikesta Quote Link to comment https://forums.phpfreaks.com/topic/187181-reorder-an-arrays-keys-but-reassociate-their-index-as-well/#findComment-988443 Share on other sites More sharing options...
sasa Posted January 5, 2010 Share Posted January 5, 2010 look array_values() function Quote Link to comment https://forums.phpfreaks.com/topic/187181-reorder-an-arrays-keys-but-reassociate-their-index-as-well/#findComment-988623 Share on other sites More sharing options...
thepip3r Posted January 5, 2010 Author Share Posted January 5, 2010 thanx sasa -- exactly what i was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/187181-reorder-an-arrays-keys-but-reassociate-their-index-as-well/#findComment-988843 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.