V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yakczh
V2EX  ›  PHP

一个 array 参数,怎么样区分是 assoc 还是 list ?

  •  
  •   yakczh · 2015-07-19 18:39:04 +08:00 · 1918 次点击
    这是一个创建于 3204 天前的主题,其中的信息可能已经有所发展或是发生改变。

    $arr=array('aa','bb','cc');

    $brr=array('1'=>'aa','2'=>'bb');

    function test ($param){

    }

    test($arr);
    test($brr);

    foccy
        1
    foccy  
       2015-07-19 21:04:18 +08:00
    不知道是什么情况下需要这样的需求。不过这里有答案: http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
    yakczh
        2
    yakczh  
    OP
       2015-07-19 23:48:19 +08:00
    [B_is_associative] => 0.26401352882385
    [B_ais_assoc] => 0.24301218986511
    [B_is_assoc] => 0.13501119613647
    [B_arrayIsAssociative] => 0.12200903892517
    [B_isNumericArray] => 0.12200570106506

    isNumericArray 最快
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2859 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 03:39 · PVG 11:39 · LAX 20:39 · JFK 23:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.