从内部数组中读取数组键

时间:2016-03-21 10:31:54

标签: php arrays

我可以从数组内部读取数组键,请建议php函数而不是foreach循环,因为我试图尽量避免循环吗?

代码如下所示:

array_fill_keys(array('a','b','c', 'd'),array(
    'action'=>'getUserLongTermCategoriesAction',
    'params'=> 'place key here',
)

1 个答案:

答案 0 :(得分:1)

检查一下。

    $arr = array('a','b','c','d');
    $temp =  array_map(function ($keys) {
        return array(
            'action'=>'getUserLongTermCategoriesAction',
            'params'=> $keys,
        );
    }, $arr);
    $result = array_combine($arr, $temp);

输出:

Array
(
    [a] => Array
        (
            [action] => getUserLongTermCategoriesAction
            [params] => a
        )

    [b] => Array
        (
            [action] => getUserLongTermCategoriesAction
            [params] => b
        )

    [c] => Array
        (
            [action] => getUserLongTermCategoriesAction
            [params] => c
        )

    [d] => Array
        (
            [action] => getUserLongTermCategoriesAction
            [params] => d
        )

)