0

Well, I am here again dealing with arrays in php. I need your hand to guide me in the right direction. Suppose the following array:

-fruits
    --green
        ---limon
        ---mango
    --red
        ---apple


-cars
    --ferrari
        ---enzo
            ----blue
            ----black
        ---318
    --lamborg
        ---spider
        ---gallardo
            ----gallado-96
                -----blue
                -----red
            -----gallado-98

The - (hyphen) symbol only illustrates the deep level.

Well, I need to build another array (or whatever), because it should be printed as an HTML select as below:

-fruits
--green
---limon
---mango
--red
---apple
-cars
--ferrari
---enzo
----blue
----black
---318
--lamborg
---spider
---gallardo
----gallado-96
-----blue
-----red
-----gallado-98

Looks that for each level element, it should add a space, or hyphen to determinate that it belongs to a particular parent.

EDIT

The have provide an answer provideng my final code. The html select element will display each level as string (repeating the "-" at the begging of the text instead multi-level elements.

4
  • Exactly how many levels do you have to your array ? It seems that you are using the wrong tool for the job. I downed your question because you have shown no effort to solve this problem by yourself. Commented Sep 29, 2012 at 23:42
  • @AlexandreP.Levasseur, no problem. There is no limit, the tree can be as many levels is required. I will update my question with my try using a recursive function. Also, the use of array is important because it already exists in the application Commented Sep 29, 2012 at 23:46
  • Your question currently lacks some real PHP code showing exactly what you already have, what is not working, and the expected output. Also, are you aware that the HTML specification currently allows only ONE level for the OPTION element? Commented Sep 30, 2012 at 1:02
  • @Jocelyn, I'm so rookie but my post now has been updates. Thank you for your feedbacks. :) Commented Sep 30, 2012 at 3:17

2 Answers 2

1

Here's a simple recursive function to build a select dropdown given an array. Unfortunately I'm not able to test it, but let me know if it works. Usage would be as follows:

function generateDropdown($array, $level = 1)
{
    if ($level == 1)
    {
        $menu = '<select>';
    }
    foreach ($array as $a)
    {
        if (is_array($a))
        {
            $menu .= generateDropdown($a, $level+1);
        }
        else
        {
            $menu .= '<option>'.str_pad('',$level,'-').$a.'</option>'."\n";
        }
    }
    if ($level == 1)
    {
        $menu = '</select>';
    }
    return $menu;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, your solution take me to the right direction :)
0

OK, I got it with the help of @jmgardhn2.

The data

This is my array:

$temp = array(
            array(
                'name' => 'fruits',
                'sons' => array(
                            array(
                                'name' => 'green',
                                'sons' => array(
                                        array(
                                            'name' => 'mango'
                                        ),
                                        array(
                                            'name' => 'banana',
                                        )
                                    )
                                )
                            )
            ),
            array(
                'name' => 'cars',
                'sons' => array(
                            array(
                                'name' => 'italy',
                                'sons' => array(
                                            array(
                                                'name' => 'ferrari',
                                                'sons' => array(
                                                        array(
                                                            'name' => 'red'
                                                        ),
                                                        array(
                                                            'name' => 'black'
                                                        ),
                                                    )
                                            ),
                                            array(
                                                'name' => 'fiat',
                                            )
                                )
                            ),
                            array(
                                'name' => 'germany',
                                'sons' => array(
                                            array(
                                                'name' => 'bmw',
                                            )
                                        )
                            ),
                )
            )    
        );

Recursive function

Now, the following function will provide an array with items like [level] => [name]:

function createSelect($tree, $items, $level)
{
    foreach ($tree as $key)
    {
        if (is_array($key))
        {
            $items = createSelect($key, $items, $level + 1);
        }
        else
        {
            $items[] = array('level' => $level, 'text' => $key);
        }
    }

    return $items;
}

Calling the funcion

Now, call the function as below:

$items = createSelect($temp, array(), 0);

Output

If you iterate the final $items array it will look like:

1fruits
2green
3mango
3banana
1cars
2italy
3ferrari
4red
4black
3fiat
2germany
3bmw

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.