Конкатенация / каскадные ключи многомерного массива

Я понял, что мне нужно перестать стучать головой и попросить помощи …

У меня есть следующий массив:

$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;
}

Есть идеи?

2

Решение

Я думаю, что это должно сделать это:

public function iterateKeys(array $array, $joiner, $prepend = NULL) {
if (!isset($formattedArray)) { $formattedArray = array(); }
foreach ($array as $key => $value) {
if(is_array($value)) {
$formatted_array = array_merge($formattedArray, $this->iterateKeys($value, $joiner, $prepend . $joiner . $key));
} else {
$formattedArray[] = $prepend . $joiner . $key;
}

}
return $formattedArray;
}

Поскольку рекурсивный вызов возвращает массив, вам нужно использовать array_merge объединить это с тем, что у вас есть. А для случая без массива вам нужно вставить новую строку в массив, а не заменить массив строкой.

2

Другие решения

попробуй это:

function flattern(&$inputArray, $tmp = null, $name = '')
{
if ($tmp === null) {
$tmp = $inputArray;
}

foreach($tmp as $index => $value) {
if (is_array($value)) {
flattern($inputArray, $value, $name.'_'.$index);

if (isset($inputArray[$index])) {
unset($inputArray[$index]);
}
} else {
$inputArray[$name.'_'.$index] = $value;
}
}

return $inputArray;
}

var_dump(flattern($permissionTypes));
2

function flattenWithKeys(array $array, array $path = []) {
$result = [];

foreach ($array as $key => $value) {
$currentPath = array_merge($path, [$key]);

if (is_array($value)) {
$result = array_merge($result, flattenWithKeys($value, $currentPath));
} else {
$result[join('_', $currentPath)] = $value;
}
}

return $result;
}

$flattened = flattenWithKeys($permissionTypes);
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector