1

I need to convert an array that is like $data = array('Image','Thumbnail','size') to $data= array('Image'=>array('Thumbnail'=>array('size'=>'test'))); format. How it will be possible?. I had tried something like below.

$count= count($data);
$last = $count-1;
for($i=$count-2;$i>=0;$i--)
{
  $newArray[$data[$i]][$data[$last]]='test'; 
  $last=$i;

}

but it gives output as

Array(
[thumbnail] => Array
    (
        [type] => test
    )

[image] => Array
    (
        [thumbnail] => test
    ))

Any help will be appreciated. Thank you :-)

3 Answers3

1

You can create your own method to create such a structure by just traversing the array and creating an inner child for each element.

function createPath($array) {
    $result = [];
    $current = &$result;
    foreach ($array as $node) {
        $current[$node] = [];
        $current = &$current[$node];
    }
    return $result;
}

This can also be done with array_reduce and an reversed array: (array_reduce doumentation, array_reverse documentation)

$result = array_reduce(array_reverse($array), function($carry, $element) {
       return [$element => $carry]; 
    }, []);

While the foreach solution will build the structure from the outer element to the most inner element, the array_reduce solution will build it from the inner to the outer elements.

Both solutions will create the output:

[
    'Image' => [
        'Thumbnail' => [
            'size' => []
        ]
    ]
]

If you want the most inner key to contain another value than an empty array, you can change the intial value of the array_reduce carry to that desired value or add another parameter to the createPath function, that adds this value as the most inner key.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
  • The first solution doesn't work. But the second solution does work and seems so magic! – Phil Jan 02 '18 at 12:37
  • @Phil Thanks for the hint. I forgot the reference operator in the first solution, which i added now. It works now. [running example](http://sandbox.onlinephpfunctions.com/code/46a9dec4250c459a28d5ed58f80ac209008e944a) – Philipp Maurer Jan 02 '18 at 12:44
  • Great job! Learn lot from this. – Phil Jan 02 '18 at 12:52
  • First solution seems to be not working but the second one is great. Thank you so much for your help @Philipp Maurer :-) – Sujith Johnson Jan 03 '18 at 04:12
1

You can use this, with very basic logic:

$array = array('Image','Thumbnail','size', 'test');
$a = [];
$i = count($array)- 1;
$a[$array[$i-1]] = $array[$i];
--$i;
while($i >= 1 ) {
    $a[$array[$i-1]] = $a;
    unset($a[$array[$i]]);
    --$i;
}
Annapurna
  • 529
  • 1
  • 6
  • 19
0

Do you have other further considerations?

The following code is simply to do so:

<?php
$data = array('Image','Thumbnail','size');
$newArray[$data[0]][$data[1]][$data[2]]='test'; 
var_dump($newArray);

Or you could use for loop like this:

<?php
$data = array('Image','Thumbnail','size');
$result=array();
for($i=(count($data)-1);$i>=0;$i--){
    if($i==(count($data)-1))
        $result[$data[$i]]='test';
    else{
        $result[$data[$i]]=$result;
        unset($result[$data[$i+1]]);
    }
}
Phil
  • 1,444
  • 2
  • 10
  • 21