我有一个关联数组,如下所示:
[
'one' => [
'two' => [
'three1' => 1,
'three2' => 'somestring',
]
] some more nodes...
我需要将其反转到以下内容:
'one.two.three1'=>1,
'one.two.three2'=>'somestring',
etc...
我可以构建第一个字符串,但是对于第二个字符串,它仅输出:
'three2'=>somestring'
但我需要它从节点的开头迭代。我的代码如下所示:
$arr2 = array();
$finalarray = array();
function rewrapback($arr, &$arr2, &$finalarray, $level=0)
{
foreach($arr as $k=>$v)
{
$startnode = $k;
if(is_array($v))
{
$level=$level+1; //I suppose I need to do something with this but I don`t know what
array_push($arr2, $k);
rewrapback($v, $arr2, $finalarray,$level);
}else
{
array_push($arr2, $k);
$string = implode(',', $arr2);
$finalarray[$string]=$v;
$arr2 = array(); //reset the string
}
}
return $finalarray;
}
关于如何解决这个问题的任何想法?谢谢