Create array from loop php
I am extremely new at php and I was wondering if someone could help me use either a Show
mickmackusa 39.1k11 gold badges76 silver badges113 bronze badges asked Nov 12, 2008 at 3:19 2
answered Nov 12, 2008 at 3:20
OwenOwen 80.8k21 gold badges117 silver badges113 bronze badges 3 A different approach to the
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. answered Nov 12, 2008 at 3:42
alexalex 467k197 gold badges865 silver badges975 bronze badges 2 a bit easier to comprehend for a beginner maybe...
answered Nov 12, 2008 at 3:48
John TJohn T 23.3k11 gold badges55 silver badges82 bronze badges 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:
while loop:
by defining it:
Or if you just wanted a range of letters or numbers:
answered Dec 17, 2008 at 5:18
nickfnickf 525k198 gold badges640 silver badges720 bronze badges The simplest way is to use
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. answered Dec 17, 2008 at 5:19
too much phptoo much php 86.4k34 gold badges127 silver badges135 bronze badges
By Parth Patel on Sep 11, 2021 Today, we will learn how to loop through an array in PHP using while ****loop, for loop, & various PHP Array functions. PHP is one of the easiest language and traversing through an array in php is very easy. Infact, most popular way to access items in array in php is using loops : while, for & do..while Let's jump into it: 1. While loopThe while loop is very common loop among all languages and PHP is not different. In fact, while loop is one of the most popular method to iterate over PHP array. Let's checkout the syntax: Syntax:
It means that, while the given expression (or condition) is true, execute the code inside the curly brackets, and check the expression again. Keep doing it, until expression becomes false. How to Iterate over PHP array using while loopThe PHP arrays have elements which can be accessed via its index position, right? So, we can use the while loop to change the index position incrementally or decrementally therefore accessing every element (or selective elements as per the condition). Let's see how it works, Example: Here, we will create an index position variable and start with 0th position which is first in an array. The condition will be to continue fetching element from an array til our index values is less than the count of array (or length of the given array). Since, while loop will not increment our index variable automatically, we need to increment it inside the loop. Therefore, with each iteration, variable will move to next index position.
Output: 2. do while LoopWell, personally it's my least favorite loop in every programming language, so I'd probably say less. The do while is another type of loop in php (and mostly all programming languages ... except some functional languages .. Yes, I am looking at you Smalltalk ) It is mostly similar to while loop, except the ordering is reverse.
Let's checkout the syntax: Syntax:
It means that, while the given expression (or condition) is true, execute the code inside the curly brackets, and check the expression again. Keep doing it, until expression becomes false. Note: Don't miss the semicolon at the end. I always forget that. Without further ado, let's recrete previous example using do while loop How to loop through php array using do while loopWe will iterate over an array of users using do while loop. Example:
Output: 3. For LoopLet's talk about the MOST popular loop. The for loop. The for loop is my most favorite general loop (next one I use often for this particular usecase) for any general iterative usecase. It does the exactly same as while loop but it has different, more compact syntax. Syntax:
It looks very complex at first, but when you get bit experience, believe me, you will prefer this most of the time. It has placeholder for three expressions (each are optional, so you can still use this loop just like while)
Let's recreate our example using for loop. How to loop through array using for loop in PHPAs shown below, it is exactly same as while loop, except it's condensed and better syntax. Also, for one line statements, you can omit curly brackets ;)
Output: 4. Foreach LoopTalking about condensed syntax, let's talk about another great and special loop - Foreach loop. This loop is only helpful when you want to iterate over each item. If for example - you want to access only even position items, then sadly this loop is not for you, you need to use above loops. But, for the particular use of accessing each item (see the pun?), foreach is my favorite and infact would be everyone's favorite. It is very simply and easy to use. Syntax:
Let's checkout the example: How to loop over array using foreach loop in PHPLet's recreate our example. We won't need the keys (position) therefore we will use foreach without keys.
Output: 5. array_walkThe array_walk is an array function provided by PHP which applies the given function to each element in the array, hence the name "array_walk". Syntax:
Parameters:
Return: It returns true when executed successfully. Let's recreate our example: Example:
Output: 6. Array IteratorThis is quite advanced and complex way to iterate over an array. Honestly, I don't see the reason why you would need to use this over other methods, but for the purpose of fun & learning, let's explore this. Here, we will create ArrayIterator object using ArrayObject function and use it to iterate over an array. It is based on pointer mechanism just like C++. Example:
Output: ConclusionWe saw 6 ways to loop over an array in PHP so far. Personally, I like to use foreach because it is very simple but you can use any of the above method. Hope, this helped. Happy Coding, Adios. How declare array in for loop in PHP?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++; } ?>
How can we store values from for loop into an array in PHP?Declare the $items array outside the loop and use $items[] to add items to the array: $items = array(); foreach($group_membership as $username) { $items[] = $username; } print_r($items); Hope it helps!!
What is the simplest way of looping through an array in PHP?Today, we will learn how to loop through an array in PHP using while ****loop, for loop, & various PHP Array functions.. While loop. ... . do while Loop. ... . For Loop. ... . Foreach Loop. ... . array_walk. ... . Array Iterator.. |