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.

mickmackusa
49k13 gold badges97 silver badges163 bronze badges
asked Jan 21, 2013 at 19:54

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,
 );
}
answered Jan 21, 2013 at 19:58
2
  • 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 ')' Commented Jan 21, 2013 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). Commented Feb 10, 2016 at 19:01
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.

answered Jan 21, 2013 at 19:56
5
  • With this approach, how would I go about making $clients be an array of arrays? sorry if this sounds a little dense! Commented Jan 21, 2013 at 20:08
  • Define $clients first [code]$clients = array(array('oneval', 'two'), array('another', 'another'));[/code] then just include $clients in your primary array Commented Jan 21, 2013 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(); Commented Jan 21, 2013 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 Commented Jan 21, 2013 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. Commented Jan 21, 2013 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

answered Jan 21, 2013 at 19:58
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
 )
];
answered Sep 19, 2024 at 16:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.