The Little Guy Posted November 9, 2007 Share Posted November 9, 2007 If I have an array that looks like this: Array ( [4] => 3 [12] => 2 [20] => 1 [23] => 1 [33] => 1 ); and I want to return the key 4, how would I get that (4 won't always be the key value)? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 9, 2007 Share Posted November 9, 2007 Just make sure your array has at least 4 values... <?php $i = 1; foreach($arr as $k=>$v) { if($i == 4) { $key = $k; break; } $i++; } echo $key; ?> Orio. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 Well, that depends on what you're trying to achieve. If you want the first key, you can reset() the array, and then use the key() function. If you want to find the key of a particular value, use the array_search() function - however, im not sure this is what you want, since you have 3 elements with a value of 1. Why are we selecting the key 4, rather than any of the others? Edit: I think the difference in the two replies highlights the ambiguity of the question! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 9, 2007 Share Posted November 9, 2007 So based on your sort you want the 4th key you say? you can run a sort that will reindex all the keys and thus $array[3] will always be it, but if the key is importat it won't work, you could try next(next(next($array); after your sort and then say get key on that there is a function for get key Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Author Share Posted November 9, 2007 here is what I thought of: <?php foreach($v as $k => $v){$total = $k; break;} ?> Is that a good way? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 9, 2007 Share Posted November 9, 2007 That will give you the first key. The script in my previous post has an if to check if this is the fourth time the loop runs and that's why it returns the 4th key. Orio. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Author Share Posted November 9, 2007 I want the first key, because I sort the array, and the one I want is first. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 9, 2007 Share Posted November 9, 2007 if you just used rsort($array) then $array[0] will gove you the highest value. (assuming you sort highest first as that is what your array looks like.) Quote Link to comment Share on other sites More sharing options...
Orio Posted November 9, 2007 Share Posted November 9, 2007 ohhh I thought the 4th lol In that case you are right, the code you posted will do the job. Orio. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Author Share Posted November 9, 2007 if you just used rsort($array) then $array[0] will gove you the highest value. (assuming you sort highest first as that is what your array looks like.) that won't work, rsort() reassigns the key values. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 Well, either use the code you posted, or use the reset() and then the key() functions. Not sure if there will be any performance difference between the two methods. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 9, 2007 Author Share Posted November 9, 2007 OK, well... I decided to use my way... here is the finished product: http://phpsnips.com/snippet.php?id=45 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 9, 2007 Share Posted November 9, 2007 If you only want the first element from an array use array_shift: case 'mode': $v = array_count_values($array); arsort($v); $total = array_shift(array_keys($v)); break; not a loop. Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 9, 2007 Share Posted November 9, 2007 This will get the 4th element of an array <?php $arr = array ( 4 => 3, 12 => 2, 20 => 1, 23 => 1, 33 => 1 ); $result = array_slice($arr , -2, 1); echo $result[0]; //returns 1 ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2007 Share Posted November 10, 2007 <?php $arr = array ( 12 => 2, 20 => 1, 4 => 3, 23 => 1, 33 => 1 ); arsort($arr); // sort descending echo current(array_keys($arr)); // first key ?> Quote Link to comment 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.