views:

447

answers:

5

I am extremely new at php and I was wondering if someone could help me use either a for or while loop to create an array 10 elements in length

+6  A: 
$array = array();
$array2 = array();

// for example
for ($i = 0; $i < 10; ++$i) {
    $array[] = 'new element';
}

// while example
while (count($array2) < 10 ) {
    $array2[] = 'new element';
}

print "For: ".count($array)."<br />";
print "While: ".count($array2)."<br />";
Owen
Attributing keys in an array via $array[] = ...; is extremely slow. It's worth having the $i from the 'for' between the square braces. $array[$i] will give you a good boost in performance when compared to $array[] if you're attributing large datasets.
I GIVE TERRIBLE ADVICE
Alternatively one can use the array_push() function
Noah Goodrich
@I GIVE... i found this to be incorrect, i ran a test based on up to 750,000 iterations and found $array[] to be faster than $array[$i] in all cases (granted the difference was a matter of thousandths of a second)
Owen
A: 

A different approach to the 'for' loop would be

    $array = array();

    foreach(range(0,9) as $i) {
        array_push($array, 'new element');
    }

    print_r($array); // to see the contents

I use this method, I find it's easier to glance over to see what it does.

As strager pointed out, it may or may not be easier to read to you. He/she also points out that a temporary array is created, and thus is slightly more expensive than a normal for loop. This overhead is minimal, so I don't mind doing it this way. What you implement is up to you.

alex
It'd be easy to read if you have a background in Python, but if you're from a C-style language background (in syntax, e.g. C++, Java, C#) this'll look strange to you. Also, the code looks inefficient, creating a temporary array.
strager
Okay, an edit is in place!
alex
+2  A: 

a bit easier to comprehend for a beginner maybe...

<?php


// for loop
for ($i = 0; $i < 10; $i++) {

$myArray[$i] = "This is element ".$i." in the array";

echo $myArray[$i];

}


//while loop
$x = 0;

while ($x < 10) {

$someArray[$x] = "This is element ".$x." in the array";

echo $someArray[$x];

$x++;
}

?>
John T
A: 

I'm not sure exactly what your purpose is here. PHP's arrays are dynamic, meaning that you can keep adding elements to them after they're created - that is, you don't need to define the length of the array at the start. I'll assume you want want to put 10 arbitrary things in an array.

for loop:

$arr = array();
for ($i = 0; $i < 10; ++$i) {
    $arr[] = "Element $i";
}

while loop:

$arr = array();
$i = 10;
while (--$i) {
    $arr[] = "Element $i";
}

by defining it:

$arr = array("Element 1", "Element 2", "Element 3" ...);

Or if you just wanted a range of letters or numbers:

$arr = range(0, 9);
$arr = range('a', 'j');
nickf
A: 

The simplest way is to use array_fill():

$array = array_fill(0, 10, 'Hello World');

But you should know that PHP arrays can be resized whenever you want anyway, I've never needed to create an array of a certain size.

too much php