PHP:设置在一个单独的数组定义的使用路径多维关联数组元素的值 [英] PHP: Set the value of a multidimensional associative array element using a path defined in a separate array
问题描述
好了,所以我有一个包含以下元素的数组:
$阵列['一'] ['B'] [0] ['C'];
$阵列['一'] ['B'] [1] ['C'];
$阵列['一'] ['D'] [0] ['C'] ['C'];
$数组['B'] ['C'];
然后在一个单独的阵列中,我定义的路径,这些值:
$结构[0] =阵列('A','B','#','C');
$结构[1] =阵列('一个','D','#','C','C');
$结构[2] =阵列('B','C');
最后,我有一个数组持有的值:
$值[0] =阵列('value0-0','value0-1');
$值[1] =阵列('value1-0');
$值[2] =阵列('value2-0');
我试图找到一个简单的功能/循环,将能够在$值应用值到在$结构中定义$数组的数组的路径。
最终的结果将是:
$阵列['一'] ['B'] [0] ['C'] ='value0-0';
$阵列['一'] ['B'] [1] ['C'] ='value0-1';
$阵列['一'] ['D'] [0] ['C'] ['C'] ='value1-0';
$数组['B'] ['C'] ='value2-0';
在$ a的情况下的值[0]或$值[1],这将是可以循环通过每个值和替代$结构元件匹配'#'与该特定$值迭代次数。
这是简单地屈服于下来,写抽出递归函数的情况下,还是有一个聪明的构造或PHP函数,可以提供更好的解决方案?
解决方案:
感谢马里奥,我最终的解决办法是:
的foreach($结构作为$ I => $键)
的foreach($值[$ i]为$ VAL){
$ R =安培; $阵列; 的foreach($键为$键){ 如果($键==#){$键= $ I; } $ R =安培; $ R [$关键]; //移动指针指向子阵列
} $ R = $ VAL;
}
}
您将有引用工作遍历目标数组:
函数注入($数组$结构,$值){ 的foreach($结构作为$ I => $键)
的foreach($值[$ i]为$ VAL){
$ R =安培; $阵列; 的foreach($键为$键){ 如果($键==#){$键=计数($ R); } 的setType($ R [$键],数组);
$ R =安培; $ R [$关键]; //移动指针指向子阵列
} $ R = $ VAL;
}
Ok so I have an array that holds the following elements:
$array['a']['b'][0]['c'];
$array['a']['b'][1]['c'];
$array['a']['d'][0]['c']['c'];
$array['b']['c'];
Then in a separate array, I have defined the path to these values:
$structure[0] = array('a','b','#','c');
$structure[1] = array('a','d','#','c','c');
$structure[2] = array('b','c');
Finally, I have an array holding the values:
$values[0] = array('value0-0','value0-1');
$values[1] = array('value1-0');
$values[2] = array('value2-0');
I'm trying to find a simple function/loop that will be able to apply the values in $values to the array path of $array that is defined in $structure.
The end result would be:
$array['a']['b'][0]['c']='value0-0';
$array['a']['b'][1]['c']='value0-1';
$array['a']['d'][0]['c']['c']='value1-0';
$array['b']['c']='value2-0';
In the case of $values[0] or $values[1], it would be able to loop through each value and substitute the $structure element matching '#' with the iteration number for that particular $value.
Is this simply a case of knuckling down and writing a drawn out recursive function, or is there a smart construct or php function that could provide a more elegant solution?
SOLUTION:
Thanks to Mario, my eventual solution is:
foreach ($struct as $i=>$keys)
foreach ($values[$i] as $val) {
$r = & $array;
foreach ($keys as $key) {
if ($key == "#") { $key = $i; }
$r = & $r[$key]; // move pointer to subarray
}
$r = $val;
}
}
You will have to work with references to traverse the target array:
function inject($array, $struct, $values) {
foreach ($struct as $i=>$keys)
foreach ($values[$i] as $val) {
$r = & $array;
foreach ($keys as $key) {
if ($key == "#") { $key = count($r); }
settype($r[$key], "array");
$r = & $r[$key]; // move pointer to subarray
}
$r = $val;
}
这篇关于PHP:设置在一个单独的数组定义的使用路径多维关联数组元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!