I have 2 array.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With