PHP - Getting the index of a element from a array

后端 未结 7 2182
既然无缘
既然无缘 2021-02-02 08:40

How can I get the current element number when I\'m traversing a array?

I know about count(), but I was hoping there\'s a built-in function for getting the current field

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 09:36

    an array does not contain index when elements are associative. An array in php can contain mixed values like this:

    $var = array("apple", "banana", "foo" => "grape", "carrot", "bar" => "donkey");   
    print_r($var);
    

    Gives you:

    Array
    (
        [0] => apple
        [1] => banana
        [foo] => grape
        [2] => carrot
        [bar] => donkey
    )
    

    What are you trying to achieve since you need the index value in an associative array?

提交回复
热议问题