yakczh
V2EX  ›  PHP

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

  •  
  •   yakczh · Jul 19, 2015 · 2447 views
    This topic created in 3997 days ago, the information mentioned may be changed or developed.

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

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

    function test ($param){

    }

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

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

    isNumericArray 最快
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4978 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:46 · PVG 17:46 · LAX 02:46 · JFK 05:46
    ♥ Do have faith in what you're doing.