0

Lets say I have an array like:

Array
(
[1] => Array
    (
        [name] => 85 x 55mm
        [options] => Array
            (
                [2] => Array
                    (
                        [2] => Ivory Board
                        [3] => Silk Board
                    )

                [6] => Array
                    (
                        [4] => 330gsm
                        [5] => 400gsm
                    )

            )

    )
)

The inner options could have x number of values. In this example I want a select list that will chain the option ids from the root parent key (could be more than 1 parent but only 1 in this case) and the values excluding the parent name i.e

<h1>85 x 55mm</h1>
<select name="1">
<option value="1,2,4">Ivory Board, 330gsm</option>
<option value="1,3,5">Silk Board, 400gsm</option>
</select>

Tried re-keying the arrays but struggling to combine.

2
  • are you sure you need the brackets around the index values in your array ?
    – collapsar
    Commented Jul 22, 2013 at 14:24
  • is it possible for you to re-arrange your array if needed ? Commented Jul 22, 2013 at 14:27

2 Answers 2

0

I have a complex array of track information from iTunes where the key value I want to cycle through is called "tracks". the number of properties associated with each track varies.

To cycle through the array I use a foreach loop.

$trackArray = (array_keys)$plist['Tracks']));

This set the key I am interested in. Then I use this loop.

    foreach ($trackArray as $value){
$trackID = ($plist['Tracks'][$value]['TrackID');
$artist = ($plist['Tracks'][$value]['artist']);
}

It took me a lot of playing around to get the right solution and I found the var_dump() and print_r() functions invaluable.

I think with a bit of playing you could get what you wanted using what I used above.

Let me know how you get on. If you still get stuck if I could have a bit more code perhaps I can knock something up.

S

0
0

The following restructures your array, grouping option characteristics together:

$arr = array (
    1 => array (
            'name' => '85 x 55mm',
            'options' => array (
                    2 => array (
                            2 => 'Ivory Board',
                            3 => 'Silk Board'),
                    6 => array (
                            4 => '330gsm',
                            5 => '400gsm'))));
$out = array();
foreach ($arr as $key => $value){
    $out[$key]['name'] = $value['name'];
    $ops = $value['options'];
    array_unshift($ops, null);
    $ops = call_user_func_array('array_map', $ops);
    $out[$key]['options'] = $ops;
}

Output:

Array(
    [1] => Array(
            [name] => 85 x 55mm
            [options] => Array(
                    [0] => Array(
                            [0] => Ivory Board
                            [1] => 330gsm
                        )
                    [1] => Array(
                            [0] => Silk Board
                            [1] => 400gsm
                        )
                )
        )
)

The following creates select menus for each item in your array (in your example, there's just one item):

$select = '';
foreach ($out as $key => $value){
    $select .= "<h1>".$value['name']."</h1>\n";
    $select .= "<select name='".$key."'>\n";
    foreach ($out[1]['options'] as $key2 => $value2){
        $select .= "<option value=''>".implode(', ', $value2)."</option>\n";
    }
    $select .= "</select>\n";
}

Output:

<select name='1'>
<h1>85 x 55mm</h1>
<option value=''>Ivory Board, 330gsm</option>
<option value=''>Silk Board, 400gsm</option>
</select>

I couldn't figure out where your values in the option tags were coming from, so I had to leave that part blank.

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.