ryanfait Posted May 16, 2008 Share Posted May 16, 2008 Is there a function or simple way to reset the array keys? For example convert the first array into the second. Array ( [0] => apple [9] => banana [11] => orange [15] => pear [20] => strawberry [21] => mango [29] => raspberry [67] => apricot ) to Array ( [0] => apple [1] => banana [2] => orange [3] => pear [4] => strawberry [5] => mango [6] => raspberry [7] => apricot ) Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/ Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 I think there is but I can't think of it right now this will due for you <?php function array_smoosh($array){ if(is_array($array)){ $data =array(); foreach($array as $value){ $data[] = $value; } return $array; } else{ return NULL; } } #to use say $new_array = array_smoosh($myarray); ?> Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542634 Share on other sites More sharing options...
ryanfait Posted May 16, 2008 Author Share Posted May 16, 2008 Thanks. It was confusing me for a second as to why it wasn't working. Should be "return $data" but it does exactly what I wanted. Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542639 Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 sorry about that missed that Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542641 Share on other sites More sharing options...
thebadbad Posted May 16, 2008 Share Posted May 16, 2008 array_values() is what you're looking for. Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542683 Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 array_values() is what you're looking for. I like array_smoosh() better Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542849 Share on other sites More sharing options...
thebadbad Posted May 16, 2008 Share Posted May 16, 2008 That's because you're a cool dude. I tend to stick with the not-so-fancy solutions Link to comment https://forums.phpfreaks.com/topic/105880-resetting-array-keys/#findComment-542925 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.