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
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,
);
}
-
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– user3199402013年01月21日 20:34:48 +00:00Commented 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).dhaupin– dhaupin2016年02月10日 19:01:18 +00:00Commented Feb 10, 2016 at 19:01
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.
-
With this approach, how would I go about making $clients be an array of arrays? sorry if this sounds a little dense!user319940– user3199402013年01月21日 20:08:35 +00:00Commented 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 arrayste– ste2013年01月21日 20:13:05 +00:00Commented 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();user319940– user3199402013年01月21日 20:17:25 +00:00Commented 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 orderste– ste2013年01月21日 20:21:18 +00:00Commented 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.user319940– user3199402013年01月21日 21:01:01 +00:00Commented Jan 21, 2013 at 21:01
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
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
)
];