Reputation: 115
I am trying to create an php array elements to be nested.
For example:
I have this array:
array(3) {
[0]=>
string(9) "Example 1"
[1]=>
string(9) "Example 2"
[2]=>
string(9) "Example 3"
}
and I want the output to be like
array(1) {
[0]=>
string(9) "Example 1"
array(1) {
[0]=>
string(9) "Example 2"
array(1) {
[0]=>
string(9) "Example 3"
}
I tried with foreach() but without success. Can someone help me?
Thanks.
Upvotes: 0
Views: 40
Reputation: 6519
$array = array("Example 1","Example 2","Example 3");
$x = count($array) - 1;
$temp = array();
for($i = $x; $i >= 0; $i--)
{
$temp = array($array[$i] => $temp);
}
echo'<pre>';
print_r($temp);
Array
(
[Example 1] => Array
(
[Example 2] => Array
(
[Example 3] => Array
(
)
)
)
)
Upvotes: 1