Forum Moderators: coopster

Message Too Old, No Replies

Array reorganize

         

orion_rus

8:48 am on Sep 17, 2006 (gmt 0)

10+ Year Member



I have array
(
'one',
'two',
'free',
'four',
'five'
)

i need to get array:
$array['one']['two']['free']['four']='five';
how i can do it?
Thanks in advance

coopster

7:48 pm on Sep 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If it is always five indexes you could just get the values from a list.
<pre> 
<?php
$array = array('one', 'two', 'free', 'four', 'five');
print_r($array);
list($one, $two, $free, $four, $five) = $array;
$array = array(); // reset
$array[$one][$two][$free][$four] = $five;
print_r($array);
</pre>

orion_rus

4:37 pm on Sep 18, 2006 (gmt 0)

10+ Year Member



i can't say how much variables excatly i have in a array

coopster

10:26 pm on Sep 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



But you always want the last one to be the value and the other values to be the multi-dimensional keys?

orion_rus

11:52 am on Sep 19, 2006 (gmt 0)

10+ Year Member



yeap

coopster

5:19 pm on Sep 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use an eval() as long as you know what values are going to be in the array, that is, as long as they are safe. I'm assuming you are since you are using the array values as indexes in your new multi-dimensional array ...
<pre>  
<?php
$array = array('one', 'two', 'free', 'four', 'five');
print_r($array);
$value = array_pop($array);
eval("\$new_array['" . implode("']['", $array) . "'] = \$value;");
print_r($new_array);
?>
</pre>