PHP - Getting the index of a element from a array

后端 未结 7 2242
既然无缘
既然无缘 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:23

    function Index($index) {
        $Count = count($YOUR_ARRAY);
        if ($index <= $Count) {
            $Keys = array_keys($YOUR_ARRAY);
            $Value = array_values($YOUR_ARRAY);
            return $Keys[$index] . ' = ' . $Value[$index];
        } else {
            return "Out of the ring";
        }
    }
    
    echo 'Index : ' . Index(0);
    

    Replace the ( $YOUR_ARRAY )

提交回复
热议问题