14

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 - any help is much appreciated. $clients is defined elsewhere, so that is not the problem.

user319940
  • 3,267
  • 8
  • 38
  • 53

3 Answers3

25

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,
    );
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 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 ')' – user319940 Jan 21 '13 at 20:34
  • 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). – dhaupin Feb 10 '16 at 19:01
  • Thank you for the simple and very clear way of explaining. – Tariq Ahmed Jul 01 '20 at 21:09
1

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.

ste
  • 286
  • 1
  • 2
  • 9
  • With this approach, how would I go about making $clients be an array of arrays? sorry if this sounds a little dense! – user319940 Jan 21 '13 at 20:08
  • Define $clients first [`code`]$clients = array(array('oneval', 'two'), array('another', 'another'));[/`code`] then just include $clients in your primary array – ste Jan 21 '13 at 20:13
  • but I need to get the values by using a foreach loop as clients is used for a wordpress query: $clients = get_users(); – user319940 Jan 21 '13 at 20:17
  • 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 – ste Jan 21 '13 at 20:21
  • 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. – user319940 Jan 21 '13 at 21:01
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

Cody Covey
  • 1,060
  • 6
  • 9