[thelist] iterating over two-dimensional arrays

Seyon evoltlist at delime.com
Sun Jul 4 22:59:38 CDT 2004


Message from Ashley Graham (7/4/2004 10:04 PM)
>hey,
>
>lets say I have a two dimensional array:
>
>$styles = array(
>                'layout' => array('simple', 'clean'),
>                'size' => array('small', 'default', 'large'),
>                'face' => array('arial', 'verdana', 
> 'trebuchet_ms')             );
>
>then, let's say I want each "sub"-array to echo out a form element, 
>specifically a select/option element, where each of the values of the 
>"sub"-array is an option.


Try something like this (pseudo-code in some places):

foreach ($styles as $foo) // iterates through each set of sub-arrays in the 
array
{
    echo <select name="$foo[$key];
    $arraylen = count($foo); // checks size of the current sub-array
    for($i = 0; $i < $arraylen; $i++)
      { // iterates through each entry within the sub-array
      echo <option name="$foo[$i]">$foo[$i]</option> // writes your option 
statement
      }
    echo </select>;
}

Might be a bit rough around the edges but that's the gist of it.

FYI, I don't think what you've got is called a two-dimensional array, it's 
a multi-dimensional array.

regards.
-marc

--
Trinidad Carnival in all its photographic glory. Playyuhself.com
http://www.playyuhself.com/


More information about the thelist mailing list