【发布时间】:2015-01-03 03:10:43
【问题描述】:
我意识到我需要停止敲打我的头并寻求帮助......
我有以下数组:
$permissionTypes = array(
'system' => array(
'view' => 'View system settings.',
'manage' => 'Manage system settings.'
),
'users' => array(
'all' => array(
'view' => 'View all users.',
'manage' => 'Manage all users.'
)
),
'associations' => array(
'generalInformation' => array(
'all' => array(
'view' => 'View general information of all associations.',
'manage' => 'Manage general information of all associations.'
),
'own' => array(
'view' => 'View general information of the association the user is a member of.',
'manage' => 'Manage general information of the association the user is a member of.'
)
)
));
我正在尝试将键折叠/级联成一维数组,如下所示:
array(
'system_view',
'system_manage',
'users_all_view',
'users_all_manage',
'associations_generalInformation_all_view',
'associations_generalInformation_all_manage',
'associations_generalInformation_own_view',
'associations_generalInformation_own_manage'
)
我可以使用嵌套循环,但数组将是未定义的维数。
这是我得到的最接近的:
public function iterateKeys(array $array, $joiner, $prepend = NULL) {
if (!isset($formattedArray)) { $formattedArray = array(); }
foreach ($array as $key => $value) {
if(is_array($value)) {
array_push($formattedArray, $joiner . $this->iterateKeys($value, $joiner, $key));
} else {
$formattedArray = $prepend . $joiner . $key;
}
}
return $formattedArray;
}
有什么想法吗?
【问题讨论】:
-
使用递归来处理任意级别的嵌套。
-
@rpaskett:我对简单地展平阵列不感兴趣。我想将子数组键连接到父数组键上。请看示例。
-
@Barmar:这就是我正在尝试的。我已经添加了我的示例代码。你知道我哪里出错了吗?
标签: php arrays multidimensional-array dynamic-arrays