在PHP中创建具有不确定深度的多维数组

时间:2017-09-05 03:34:13

标签: php arrays function multidimensional-array

晚上好 - 我正在尝试根据存储在数据库中的帐户代码的分解文本字符串创建一个多维数组。帐户代码将具有不同的长度/深度。比如说下面数组中的$ test类似于我从数据库中提取的结果:

[110] => Array
    (
        [5100] => Array
            (
                [120] => Teacher Salaries
                [130] => Other Professional Services
                [510] => Array
                    (
                        [1] => Primary Supplies
                        [2] => Intermediate Supplies
                    )
            )
        [7300] => Array
            (
                [110] => Administrator Salaries
                [510] => Supplies
            )
    )
[763] => Array
    (
        [5100] => Academic Grants
    )

我想要做的是创建一个返回数组的函数,如下所示:

function expandedArray($codes, $value) { 
    $decoded = explode("|",$codes);
    RETURN expandedArraySub($decoded, $value); 
}
function expandedArraySub($decoded = array(), $value = Null) { 
    $k = array_pop($decoded); 
    $out[$k] = $value;
    if(is_array($decoded) && count($decoded) > 0) { $out = expandedArraySub($decoded, $out); }
    RETURN $out;
}

我能够提出一个函数,它可以将一个代码中的一个代码正确地分解成一个数组,但是当我尝试将它们合并在一起时,它们会丢失它们的密钥。以下是我到目前为止的情况:

$r1 = expandedArray('110|5100|510|1', "Primary Supplies"); 
  // $r1 is now [ 110 => [5100 => [510 => [ 1 ] ] ] ]
$r2 = expandedArray('110|5100|510|2', 'Intermediate Supplies'); 
  // $r2 is now [ 110 => [5100 => [510 => [ 2 ] ] ] ]
$r = array_merge($r1, $r2); 

但是当我运行以下内容时,我得到的数据丢失了“110”键,而不是像上面所描述的那样得到我想要的内容:

[0] => Array
    (
        [5100] => Array
            (
                [510] => Array
                    (
                        [1] => Primary Supplies
                    )
            )
    )
[1] => Array
    (
        [5100] => Array
            (
                [510] => Array
                    (
                        [2] => Intermediate Supplies
                    )
            )
    )

我得到的结果删除了第一个键,并没有像我希望的那样结合结果。这是我得到的:

{{1}}

非常感谢任何帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

试试这段代码:

function getVal($data,$chain){
    $level = $data;
    for($i=0;$i<count($chain);$i++){
        if(isset($level[$chain[$i]]))
            $level = $level[$chain[$i]];
        else
            return null; // key does not exist, return null
    }
    return $level;
}

function setVal(&$data,$chain,$value){
    $level = &$data;
    for($i=0;$i<count($chain);$i++){
        $level = &$level[$chain[$i]]; // set reference (&) in order to change the value of the object
    }
    $level = $value;
}

设置使用方式如下:

$output = array();
$test = array( 
    '110|5100|120'    => 'Teacher Salaries',
    '110|5100|130'    => 'Other Professoinal Services',
    '110|5100|510|1'  => 'Primary Supplies',
    '110|5100|510|2'  => 'Intermediate Supplies', 
    '110|7300|110'    => 'Administrator Salaries', 
    '110|7300|510'    => 'Administrative Supplies', 
    '763|5100'        => 'Academic Grants'
); 
foreach($test AS $k => $v) { 
    $lvl = explode("|", $k);
    setVal($output,$lvl,$v); 
}

$output应该具有所需的格式。

my previous post

中详细了解此代码