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.
    }
4
  • 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. Commented Dec 21, 2015 at 10:03
  • Possible duplicate of Variable containing a path as a string to multi-dimensional array? Commented Dec 21, 2015 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. Commented Dec 21, 2015 at 10:12
  • @dtn checkout my answer if it helps.. Commented Dec 21, 2015 at 11:35

1 Answer 1

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
    )

 )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.