1

Can I read array key from inside the array itself , please to suggest php functions and not foreach loops , as I am trying to avoid loops as much as possible?

code looks like this :

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

1 Answer 1

1

Check this.

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

Output:

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to keep array keys using array_fill_keys as u see so keys are a,b,c,d . is it possible in simple way to achieve that too ? @walkingRed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.