This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

1963 points
51ae1337473073ad4c003ff7_675251126
Submitted by
Nrrs
about 4 years ago

Solved with a for loop

Instead of typing five times array_push I created an Array with elements inside. I used a for loop to iterate through it and for everey item found in $animal it pushed it over to the empty array. Kind of a clone loop. I guess there's some kind of clone method, but I used this to experiment and it worked :)

<?php
// Create an array and push 5 elements on to it, then 
// print the number of elements in your array to the screen
$arrNum = array();
$animal = array("hund","katze","maus","hase","bieber");

for($i = 0; $i < count($animal); $i++) {
    array_push($arrNum, $animal[$i]);
}
echo count($arrNum);
?>

1 vote

permalink

i want to do it in a loop way? but why doesn;t it work?could u help me?


<?php
$s = array();
for (i = 0:i<6 :i++){
array_push($s,i);

print count($s);
}

?>
</p>

675 points
500ca724ed132b0002002830_376919016
Submitted by
R Zhang
almost 4 years ago


0 votes

permalink

another solution is

<?php
    // Create an array and push 5 elements on to it, then 
   // print the number of elements in your array to the screen
$fuckery = array();
$x = 0;
do {
     array_push($fuckery, rand());
     $x++;
} while($x < 5);
print count($fuckery);
?>

756 points
519020e9b3f05cce6c000a24_326795451
Submitted by
xHav0kx
about 3 years ago


0 votes

permalink

Or could just do:

array_push($food,"Pizza","Strudel","Curry","Cheescake","Pattie");

468 points
01616a68ffe7e4b66fc4d05dc08bf26e?s=140&d=retro
Submitted by
Rich
over 2 years ago