簡體   English   中英

PHP如何將單個數組轉換為多維數組?

[英]PHP how to convert an single array to multidimentional array?

我只有一個數組,我需要在多維數組中進行轉換,而無需使用array_mergearray_replace_recurcive等,只是一個自治函數:

$single = [
    0 => 'one',
    1 => 'two',
    2 => 'tree',
    3 => 'four',
    4 => 'five'
];

並轉換為如下形式,最后一個鍵為值:

$multidimentional = [
    'one' => [
        'two' => [
            'tree' => [
                'four' => 'five'
            ]
        ]
    ]
];

如果有幫助,我可以創建一個遞歸函數:

function array_replace_recursive($defaults, $replaces) {

    if(is_null($replaces)) {
        $replaces = [];
    }

    if(!is_array($defaults) || !is_array($replaces)) {
        return $replaces;
    }

    foreach($defaults as $key => $value) {
        if(!array_key_exists($key, $replaces) || is_null($replaces[$key])) {
            $replaces[$key] = $value;
        } else {
            if(is_array($replaces[$key]) && is_array($value)) {
                $replaces[$key] = array_replace_recursive($replaces[$key], $value);
            }
        }
    }

    return $replaces; 
}

您可以使用PHP(自5.6版開始)的參數解壓縮和一個非常簡單的遞歸函數來完成此操作:

$single = [
    0 => 'one',
    1 => 'two',
    2 => 'tree',
    3 => 'four',
    4 => 'five'
];

function convert($key, ...$values) {
    if (count($values) == 1)
        return array($key => $values[0]);
    else
        return array($key => convert(...$values));
}

print_r(convert(...$single));

輸出:

Array
(
    [one] => Array
        (
            [two] => Array
                (
                    [tree] => Array
                        (
                            [four] => five
                        )
                )
        )
)

您也可以不使用count (僅isset )來執行此操作:

function convert2($key, ...$values) {
    if (!isset($values[1]))
        return array($key => $values[0]);
    else
        return array($key => convert(...$values));
}

print_r(convert2(...$single));

輸出:

Array
(
    [one] => Array
        (
            [two] => Array
                (
                    [tree] => Array
                        (
                            [four] => five
                        )
                )
        )
)

遞歸考慮,您可以編寫一個基本案例,如果當前所見項目的值比數組的長度小一,則返回該值。

$singleDim = [
    0 => 'one',
    1 => 'two',
    2 => 'tree',
    3 => 'four',
    4 => 'five'
];

function toMultiDimArray($arr, $seen=0) {
    if ([] === $arr) {
        return [];
    }

    if(count($arr) - 1 === $seen) {
        return $arr[$seen];
    }

    return [
       $arr[$seen] => toMultiDimArray($arr, $seen+1)
    ];
}

$multiDim = toMultiDimArray($singleDim);

var_dump($multiDim);

array(1) {
  ["one"]=>
  array(1) {
    ["two"]=>
    array(1) {
      ["tree"]=>
      array(1) {
        ["four"]=>
        string(4) "five"
      }
    }
  }
}
$single = [
    0 => 'one',
    1 => 'two',
    2 => 'tree',
    3 => 'four',
    4 => 'five'
];

function to_multidimentional($array, $count = 0, $seen = 0) {

    if($count - 1 === $seen) {
        return $array[$seen];
    }

    return [
       $array[$seen] => to_multidimentional($array, $count, $seen + 1)
    ];
}

$single = to_multidimentional($single, count($single));

print_r($single);

exit;

對此沒有任何遞歸。 只是向后遍歷$single數組,然后每次將結果嵌入到新鍵中:

function foo( $single ) {
    $multidimensional = [ $single[ count($single) - 1 ] ];

    for( $i = count($single) - 2; $i >= 0; $i--  ) {
      $multidimensional = [ $single[$i] => $multidimensional ];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM