PHP - 连接/级联多维数组键

时间:2015-01-03 03:10:44

标签: php arrays multidimensional-array dynamic-arrays

我意识到我需要不停地敲打头并寻求帮助......

我有以下数组:

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

有什么想法吗?

3 个答案:

答案 0 :(得分: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将它与您当前拥有的数组合。对于非数组的情况,您需要将新字符串推送到数组,而不是用字符串替换数组。

答案 1 :(得分: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 :(得分:0)

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);