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? 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] 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); Link to comment https://forums.phpfreaks.com/topic/15046-array-question/#findComment-60516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.