Joe Haley Posted July 19, 2006 Share Posted July 19, 2006 Lets say i have a line of code:$array[] = 'whatever';Now lets say that i need the [u]key[/u] of the element i just added to the array set to a variable or return.How would i do this? Quote Link to comment https://forums.phpfreaks.com/topic/15046-array-question/ Share on other sites More sharing options...
GingerRobot Posted July 19, 2006 Share Posted July 19, 2006 This is pretty much the example on the php website, but i wanted to try it out. The original is on here:http://uk2.php.net/manual/en/function.key.php[code]<?php$array[] = 'whatever';while ($array_test = current($array)) { if ($array_test == 'whatever') { echo key($array); } next($array);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15046-array-question/#findComment-60507 Share on other sites More sharing options...
Joe Haley Posted July 19, 2006 Author Share Posted July 19, 2006 Seems a little... complex.I did think of something, however:$key = count($array);if ($key != 0)$key--;$array[$key] = 'whatever';return($key);as it is just a numericly indexed array.Edit:ooohhh, key() is just what is needed!$array[] = '';end($array);return key($array); Quote Link to comment https://forums.phpfreaks.com/topic/15046-array-question/#findComment-60516 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.