内部数组foreach循环 [英] Foreach loop inside array
本文介绍了内部数组foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个数组里的数组,使用for循环 - 这里是我的code:
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
解析错误:语法错误,意想不到的T_CONSTANT_ENCAPSED_STRING,
预计')'。
"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'"
有关该行:
'options' => array(
我在一个有点损失,以什么错误 - 任何帮助是非常AP preciated。 $客户在其他地方定义,所以这是没有问题的。
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.
推荐答案
这是无效的语法。你必须首先构建阵列的父部分。然后在子阵列的东西与foreach循环添加:
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,
);
}
这篇关于内部数组foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文