1

I have an array containing a lot category strings like

Cars > Model1
Cars > Model1 > Accessories
Cars > Model1 > Something
Cars > Model2
Cars > Model3

which I want to recursively transform into a multidimensional array where "Cars" is the parent element of Model1, Model2 and Model3.

And of course, "Accessories" and "Something" would be child elements of Model1.

Have been trying to work out the logic behind this for quite some time now, and I just can't seem to figure it out...

Edit: What I have so far, it's not much. As mentioned, can't seem to figure out the logic behind how to develop this.

$array = array(
        'Cars > Model1',
        'Cars > Model1 > Accessories',
        'Cars > Model1 > Something',
        'Cars > Model2',
        'Cars > Model3'
    );

    for ($i = 0; $i < count($array); $i++) {
        $name = explode('>', $array[$i]);

        // somehow check if the current "name" is already pushed to a new array - if it is, then add as a child.
    }
dtn
  • 63
  • 7
  • I have tried exploding ">" and looping through the exploded string to push this into a $newArray. But within each iteration, I need to check if this particular name is already inside the $newArray - because then we won't be adding to it. – dtn Dec 21 '15 at 10:03
  • Possible duplicate of [Variable containing a path as a string to multi-dimensional array?](http://stackoverflow.com/questions/3857033/variable-containing-a-path-as-a-string-to-multi-dimensional-array) – Mihai Matei Dec 21 '15 at 10:04
  • Added the small piece of code I have thus far. @MateiMihai, I don't think this solution would solve it for me. I need "Cars" to be a unique key and add the different "Models" under it as a multidimensional array. – dtn Dec 21 '15 at 10:12
  • @dtn checkout my answer if it helps.. – Basheer Kharoti Dec 21 '15 at 11:35

1 Answers1

0

Here's how you can achieve this

$array_data = array(
    'Cars > Model1',
    'Cars > Model1 > Accessories',
    'Cars > Model1 > Something',
    'Cars > Model2',
    'Cars > Model3'
);

$last_key = null;
$array = [];
$result = [];
foreach($array_data as $key => $values)
{
  $data = explode('>', $values);
  if(is_array($data))
  {
    $count = 0;
    $pop = array_pop($data);
    foreach($data as $k => $v)
    {
        if($count == 1)
        {
            unset( $result[$last_key] );
            $result[$last_key][$v] = $pop;
            $count++;
            continue;
        }
        $last_key = $v;
        $result[$v] = $pop;
        $count++;
    }
    $array[] = $result;
  }
 }

and the result will be something like

echo '<pre>';print_r( $array );
Array
(
  [0] => Array
    (
        [Cars ] =>  Model1
    )

[1] => Array
    (
        [Cars ] => Array
            (
                [ Model1 ] =>  Accessories
            )
    )

 [2] => Array
    (
        [Cars ] => Array
            (
                [ Model1 ] =>  Something
            )

    )

[3] => Array
    (
        [Cars ] =>  Model2
    )

[4] => Array
    (
        [Cars ] =>  Model3
    )

 )
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50