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

时间:2021-03-28 10:49:10

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?

什么好主意吗?

3 个解决方案

#1


2  

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.

由于递归调用返回一个数组,所以需要使用array_merge将其与当前的数组合并。对于非数组情况,需要将新的字符串推到数组中,而不是用字符串替换数组。

#2


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


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

#1


2  

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.

由于递归调用返回一个数组,所以需要使用array_merge将其与当前的数组合并。对于非数组情况,需要将新的字符串推到数组中,而不是用字符串替换数组。

#2


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


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