thiggins09 Posted July 27, 2007 Share Posted July 27, 2007 Hi, I have an array and each key in it is labled a user's ID. I was wondering what the most efficient way to get the real place of that key would be. For example: $arr[342]=34; $arr[235]=55; $arr[164]=64; $arr[766]=4; key 342 is first in the array, 235 second an so on. So if I wanted to tell that 744 is really fourth in the array, what would be the best way? Link to comment https://forums.phpfreaks.com/topic/61948-solved-quickest-way-to-tell-key/ Share on other sites More sharing options...
teng84 Posted July 27, 2007 Share Posted July 27, 2007 you can use foreach is that? Link to comment https://forums.phpfreaks.com/topic/61948-solved-quickest-way-to-tell-key/#findComment-308472 Share on other sites More sharing options...
thiggins09 Posted July 27, 2007 Author Share Posted July 27, 2007 I know that would do it but I was hoping there was a faster way. Link to comment https://forums.phpfreaks.com/topic/61948-solved-quickest-way-to-tell-key/#findComment-308473 Share on other sites More sharing options...
hitman6003 Posted July 27, 2007 Share Posted July 27, 2007 There isn't a specific function to find what you want, you'll have to loop through the array... <?php $arr[342]=34; $arr[235]=55; $arr[164]=64; $arr[766]=4; $key_to_find = 164; $cnt = 1; while ($values = each($arr)) { if ($values['key'] == $key_to_find) { $place = $cnt; break; } $cnt++; } echo $place; ?> Link to comment https://forums.phpfreaks.com/topic/61948-solved-quickest-way-to-tell-key/#findComment-308492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.