【问题标题】:get automatically assigned key when setting array value [duplicate]设置数组值时自动分配键[重复]
【发布时间】:2012-12-14 09:16:20
【问题描述】:

可能重复:
PHP get index of last inserted item in array

我的数组是通过以下方式生成的:

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

如何获取自动分配的键[]的数值?

【问题讨论】:

    标签: php


    【解决方案1】:
    <?php $numberOfFailedUrls = count($_SESSION['add_fail_urls']); ?>
    

    应该做的伎俩。如果您想确保不会收到关于 add_fail_urls 不存在的任何通知,请改用以下内容:

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

    编辑:我可能误解了您的帖子(截至@des 评论),因为您可能想要添加元素的实际索引而不是元素的数量。如果是,here's a solution brought to you from @romaninsh in the linked question:

    <?php 
        end($a);
        $last_id=key($a);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      相关资源
      最近更新 更多