由键嵌套的平面数组

时间:2015-11-22 11:47:59

标签: php arrays multidimensional-array

我正在尝试根据其键创建嵌套数组。 如果简化任务,也可以更改原始数组中的键格式。

来自:

$arr = [
        'player' => [
            'name' => 'Joe'
            , 'lastName' => 'Snow'
        ]
        ,'team' => [
            'name'=> 'Stars'
            ,'picture' => [
                'name' => 'Joe Snow Profile'
                , 'file' =>'xxx.jpg'
            ]
        ],
    ];

要:

<span ng-bind-html="myCtrl.textToTranslate|translate"></span>

5 个答案:

答案 0 :(得分:6)

这是我的看法 它应该能够处理任意深度

function unflatten($arr) {
    $result = array();

    foreach($arr as $key => $value) {
        $keys = explode(".", $key); //potentially other separator
        $lastKey = array_pop($keys);

        $node = &$result;
        foreach($keys as $k) {
            if (!array_key_exists($k, $node))
                $node[$k] = array();
            $node = &$node[$k];
        }

        $node[$lastKey] = $value;
    }

    return $result;
}

答案 1 :(得分:1)

迭代和递归的组合。可以简化为迭代。

$array = [
    'player.name' => 'Joe',
    'player.lastName' => 'Snow',
    'team.name' => 'Stars',
    'team.picture.name' => 'Joe Snow Profile',
    'team.picture.file' => 'xxx.jpg'
];

$newArray = array ();
foreach($array as $key=> $value) {
    $temp = array ();
    $keys = array_reverse (explode('.', $key));

    $temp[$keys[0]] = $value;
    for ($i = 1; $i < count($keys); $i++) {
        $temp[$keys[$i]] = $temp;
        unset ($temp [$keys [$i -1]]);
    }

    $newArray = array_merge_recursive($newArray,$temp);
}
var_dump($newArray );

答案 2 :(得分:1)

我收到这个问题作为测试,这是我的答案:

<?php
function buildArray(&$newArray, $keys, $value)
{
    if (sizeof($keys) == 0) {
        return $value;
    } else {
        $key = array_shift($keys);

        if (isset($newArray[$key])) {
            $value = buildArray($newArray[$key], $keys, $value);
            if (is_array($value)) {
                $newArray[$key] += $value;
            } else {
                $newArray[$key] = $value;
            }
            $arr = $newArray;
        } else {
            $arr[$key] = buildArray($newArray, $keys, $value);
        }
    }

    return $arr;
}


$arr = [
    'player.name' => 'Joe',
    'player.lastName' => 'Snow',
    'team.name' => 'Stars',
    'team.picture.name' => 'Joe Snow Profile',
    'team.picture.file' => 'xxx.jpg',
];

$newArray = [];
foreach ($arr as $key => $value) {

    $explodedKey = explode(".", $key);

    $temp = buildArray($newArray, $explodedKey, $value);
    $newArray = array_merge($temp, $newArray);

}
print_r($newArray);
?>

答案 3 :(得分:0)

你可以这样做

$newArr = []; 
for ($arr as $key => $val) {
    $tmp = explode ('.', $key);
    if (!array_key_exists ($tmp [0], $newArray){
        $newArray [$tmp [0]] = [];
    }
    $newArray [$tmp [0]][$tmp [1]] = $val;
 } 

修改 该死的没有看到团队中的第三级 不是非常通用但应该适用于第三级;)

$newArr = []; 
for ($arr as $key => $val) {
    $tmp = explode ('.', $key);
    if (!array_key_exists ($tmp [0], $newArray){
        $newArray [$tmp [0]] = [];
    }
    if (count($tmp) > 2){
        if (!array_key_exists ($tmp [1], $newArray[$tmp [0]]){
            $newArray [$tmp [0]][$tmp[1]] = [];
        }
        $newArray [$tmp [0]][$tmp [1]][$tmp [2]] = $val;
    } else {
        $newArray [$tmp [0]][$tmp [1]] = $val;
    }
 } 

答案 4 :(得分:0)

我认为您可以使用类似的方法将2d数组转换为嵌套树。但是你必须和parent_id一起玩

https://github.com/denis-cto/flat-array-to-nested-tree