PHP5 Fill an array with test data.

Discussion in 'Programming/Scripts' started by iseekagain, Jul 7, 2012.

  1. iseekagain

    iseekagain New Member

    Hi Everyone,

    Need some help to create an array of values ranging from "a" to "zzzzz" but have no idea where to start or look for guidance on this .

    Ideally multidimensional array under 1 letter, 2 letters, 3 letters, 4 letters and finally 5 letters.

    Can someone point me in the right direction ?

    Thanks in advance.
     
  2. arp

    arp New Member

    can be solved using loops

    If I understand you correctly, this should work fine..

    PHP:

    $alphabet 
    range('a''z');
    $array = array();

    for ( 
    $i 1$i 6$i++ ) {
        foreach( 
    $alphabet as $item ) {
            
    $array[$i][$item] = str_repeat$item$i );
        }
    }

    # show debug ouptut
    print_r $array );


    which outputs

    Code:
    Array
    (
        [1] => Array
            (
                [a] => a
                [b] => b
                [c] => c
                [d] => d
    .....
        [2] => Array
            (
                [a] => aa
                [b] => bb
                [c] => cc
                [d] => dd
                [e] => ee
                [f] => ff
                [g] => gg
                [h] => hh
                [i] => ii
                [j] => jj
                [k] => kk
                [l] => ll
                [m] => mm
                [n] => nn
                [o] => oo
    ....
    
     
  3. Qzey

    Qzey New Member

  4. sjau

    sjau Local Meanie Moderator

    I know I'm a bit late, but I like this function to display arrays:

    Code:
    function displayArray($aArray)
    {
        if (is_array($aArray) && (count($aArray) > 0))
        {
            print("<table border=1>");
            print("<tr><th>Key</th><th>Value</th></tr>");
                foreach ($aArray as $aKey => $aValue)
                {
                    print("<tr>");
                    if (!is_array($aValue))
                    {
                        if (empty($aValue))
                        {
                            print("<td>$aKey</td><td><i>$aValue</i></td>");
                        } else {
                            print("<td>$aKey</td><td>$aValue</td>");
                        }
                    } else {
                        print("<td>$aKey(array)</td><td>");
                        displayArray($aValue);
                        print("</td>");
                    }
                    print("</tr>");
                }
            print("</table>");
        } else {
            print("<i>empty or invalid</i>");
        }
    }
    
     

Share This Page