HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   PHP5 Fill an array with test data. (http://www.howtoforge.com/forums/showthread.php?t=57853)

iseekagain 7th July 2012 15:28

PHP5 Fill an array with test data.
 
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.

arp 16th August 2012 09:35

can be solved using loops
 
If I understand you correctly, this should work fine..

PHP Code:


$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
....


Qzey 24th October 2012 01:25

or you can use this method -> http://php.net/manual/en/function.array-fill.php

sjau 12th April 2013 20:27

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>");
    }
}



All times are GMT +2. The time now is 17:49.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.