Jump to content

[SOLVED] Quickest way to tell key


thiggins09

Recommended Posts

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

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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.