Jump to content

Archived

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

Joe Haley

Array Question

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

×

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.