PHP foreach that returns keys only

前端 未结 7 818
逝去的感伤
逝去的感伤 2020-12-29 18:51

Theoretical question that perhaps does not make any sense but still, maybe there is a clever answer.

I want to iterate through array and get its keys and to somethin

7条回答
  •  囚心锁ツ
    2020-12-29 19:23

    If you want to set all keys to certain value you can just do it this way:

    $array = array(
            'foo'=> 'oldval1',
            'bar'=> 'oldval2',
            'baz'=> 'oldval3'
    );
    
    $other_array = array_fill_keys(array_keys($array), 'something');
    print_r($other_array);
    

    This will produce:

    Array
    (
        [foo] => something
        [bar] => something
        [baz] => something
    )
    

提交回复
热议问题