I have 2 array.
我有2个阵列。
First Array:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
Second Array:
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
i need to change this array like
我需要改变这个数组
First array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => d
)
)
)
)
Second Array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
sirwilliam answers helped for first array problem. and I need it for multi dimensional array. Help to resolve the problem. Thanks in advance
sirwilliam答案帮助解决了第一个阵列问题。我需要它用于多维数组。帮助解决问题。提前致谢
1 个解决方案
#1
6
You can try to use & (references):
您可以尝试使用&(引用):
PHP:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
$newArray = array();
$newArray[key($array)] = array();
$part = &$newArray;
foreach($array as $first => $second){
$part = &$part[$first];
$part[$second] = array();
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
Result:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)
Then you can create a loop for the last part.
然后你可以为最后一部分创建一个循环。
#1
6
You can try to use & (references):
您可以尝试使用&(引用):
PHP:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
$newArray = array();
$newArray[key($array)] = array();
$part = &$newArray;
foreach($array as $first => $second){
$part = &$part[$first];
$part[$second] = array();
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
Result:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)
Then you can create a loop for the last part.
然后你可以为最后一部分创建一个循环。