PHP - Get key name of array value

前端 未结 9 1549
暗喜
暗喜 2020-11-29 16:53

I have an array as the following:

function example() {
    /* some stuff here that pushes items with
        dynamically created key strings into an array */         


        
9条回答
  •  渐次进展
    2020-11-29 17:27

    If the name's dynamic, then you must have something like

    $arr[$key]
    

    which'd mean that $key contains the value of the key.

    You can use array_keys() to get ALL the keys of an array, e.g.

    $arr = array('a' => 'b', 'c' => 'd')
    $x = array_keys($arr);
    

    would give you

    $x = array(0 => 'a', 1 => 'c');
    

提交回复
热议问题