15

I'm trying to create an array inside an array, using a for loop - here's my code:

array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix . 'client',
    'type' => 'radio'
    'options' => array( 
        foreach ($clients as $user) {
         $user->user_login => array (  
            'label' => $user->user_login,  
            'value' => $user->user_login,
            ), 
        }
    )
)

Unfortunately this gives me a

"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'"

For the line:

'options' => array( 

I'm at a bit of a loss as to what has gone wrong. $clients is defined elsewhere, so that is not the problem.

4 Answers 4

26

That's invalid syntax. You'd have to build the "parent" portions of the array first. THEN add in the sub-array stuff with the foreach loop:

$foo = array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio',
    'options' => array()
);

foreach ($clients as $user) {
    $foo['options'][] = array (  
        'label' => $user->user_login,  
        'value' => $user->user_login,
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this doesn't seem to work for me as the array foo, is in an array itself which gives the error Parse error: syntax error, unexpected T_FOREACH, expecting ')'
You don't need to make 'options' an array() if there is a chance it won't be filled. That may throw off some parsers to see an empty value (ie, schema json-ld).
2

You use foreach to access the data, not define it.

Try this:

array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio'
    'options' => $clients
    )

If you need to change the structure of the data for 'options', do this before defining the primary array.

5 Comments

With this approach, how would I go about making $clients be an array of arrays? sorry if this sounds a little dense!
Define $clients first [code]$clients = array(array('oneval', 'two'), array('another', 'another'));[/code] then just include $clients in your primary array
but I need to get the values by using a foreach loop as clients is used for a wordpress query: $clients = get_users();
Then make sure you've populated an array with the structure you expect and assign it to $clients - or try Marc's answer; same approach, different order
Not quite sure what you mean by that, unfortunately Marc's code does not work in my situation (see comment), thanks for your help though.
1

You cannot use the foreach in the definition of the array. You can however put the $clients variable in the array itself or you can foreach outside the array to build the array to be inserted at the options key

Comments

0

array_map() will elegantly allow you to dynamically populate the subarray without needing to break out of tour original array. Demo

$result = [
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix . 'client',
    'type' => 'radio',
    'options' => array_map( 
        fn($user) => [  
            'label' => $user->user_login,  
            'value' => $user->user_login,
        ], 
        $clients
    )
];

Comments

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.