[英]Creating a multi-dimensional array in PHP with an uncertain depth
Good Evening - I am trying to create a multi-dimensional array based on an exploded text string of a account codes stored in the database. 晚上好-我正在尝试根据存储在数据库中的帐户代码的分解文本字符串创建多维数组。 The account codes will be of varying lengths/depths. 帐户代码将具有不同的长度/深度。 Say for example $test in the array below are similar to the results I would pull from the database: 假设下面的数组中的$ test与我从数据库中提取的结果类似:
$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);
// Not sure what to do next...
}
What I want to do is create a function which will return an array such as the following: 我想做的是创建一个函数,该函数将返回如下数组:
[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
)
I was able to come up with a function that can take a single one of the codes and break it down correctly into an array, but when I try to merge them together, they lose their keys. 我能够提出一个函数,该函数可以采用单个代码并将其正确分解为一个数组,但是当我尝试将它们合并在一起时,它们将丢失其键。 Here is what I have so far: 这是我到目前为止的内容:
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;
}
But when I run the following, instead of getting what I want as described above, I get an array that loses the "110" key: 但是,当我运行以下命令时,没有得到如上所述的结果,而是得到了丢失了“ 110”键的阵列:
$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);
The results I get remove the first key and doesn't combine the results as I was hoping. 我得到的结果删除了第一个键,并且没有按我希望的那样合并结果。 Here is what I get: 这是我得到的:
[0] => Array
(
[5100] => Array
(
[510] => Array
(
[1] => Primary Supplies
)
)
)
[1] => Array
(
[5100] => Array
(
[510] => Array
(
[2] => Intermediate Supplies
)
)
)
Any help is much appreciated! 任何帮助深表感谢! Thanks! 谢谢!
Try this code: 试试这个代码:
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;
}
For setting use it like this: 要进行设置,请按以下方式使用它:
$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
should have the desired format. $output
应该具有所需的格式。
Read more about this code in my previous post 在我以前的文章中阅读有关此代码的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.