0

Possible Duplicate:
PHP get index of last inserted item in array

My array is generated the following way:

$_SESSION['add_fail_urls'][] = $_REQUEST['url'];

How do I get a number value of the automatically assigned key []?

0

1 Answer 1

1
<?php $numberOfFailedUrls = count($_SESSION['add_fail_urls']); ?>

Should do the trick. If you want to ensure that you don't get any notices about the add_fail_urls not existing, then use the following instead:

<?php 
    $numberOfFailedUrls = isset($_SESSION['add_fail_urls']) ? 
        count($_SESSION['add_fail_urls']) : 
        0; 
?>

Edit: I might've misinterpreted your post (as of @des comment), as you might want the actual index of the added element instead of the number of elements. If so, here's a solution brought to you from @romaninsh in the linked question:

<?php 
    end($a);
    $last_id=key($a);
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.