PHP - Concatenate / cascade multidimensional array keys

女生的网名这么多〃 提交于 2019-12-11 03:21:59

问题


I've realized I need to stop banging my head and ask for help...

I have the following array:

$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.'
                )
            )
    ));

I'm trying to collapse / cascade the keys into a one-dimension array like so:

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

I could use nested loops, but the array will be an undefined number of dimensions.

This is the closest I've gotten:

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

Any ideas?


回答1:


I think this should do it:

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

Since the recursive call returns an array, you need to use array_merge to combine it with what you currently have. And for the non-array case, you need to push the new string onto the array, not replace the array with a string.




回答2:


try this:

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



回答3:


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


来源:https://stackoverflow.com/questions/27751362/php-concatenate-cascade-multidimensional-array-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!