How to store values in array in php using while loop
You need to set value properly. Show
The more readable code would be:
And would result in
And key-less soulution would be:
And would result in PHP, just like most other programming languages has multiple ways to loop through arrays. The most popular ways to do it usually is with a 1. While LoopThe while loop is probably the most popular because of the recognizable and meaningful name. I always like to think of the while loop as the following.
This method is commonly used when working with database results in the style of arrays but at the same time, are a completely feasible way to read non-database result-arrays. With a plain array, we can create a true or false value depending on if the array has been looped over till the end. To achieve this, another variable must be
created, an index indicator variable that does two things, allows us to pick out each of the values in the array as well as instructing the while loop when to stop. It’s somewhat very similar to how a PHP Code
Output
2. For LoopAs demonstrated in the last section for
The start of the loop will always look similar to the following –
What’s really important to note here is that you must use semi-colons after the first two
parameters otherwise the code will throw an exception. Also, there is no need to increment the counter within the loop, as the PHP Code
Output
3. Foreach LoopThis type of loop is my personal favorite way to read arrays, there is no need for a PHP Code
Output
As you can see within the code example, there is much less fluff in terms of code. And, if you name your variables well, the code will make a lot more sense than other looping methods. 4. Do While LoopYou could almost say the With the do while loop, you will need to create a test within the PHP Code
Output
As you can see in the code, there is a need to create an increment index value that will work part as the test and part to access values from the array. 5. Array IteratorNow, this is a slightly more advanced method of looping over arrays is called an PHP Code
Output
Function ReferencesHere are the links to the documentation on each of the loops used within this article. Check them out as they are extremely informative and useful for bookmarking for reference later.
SummaryWith the demonstration of five ways to loop over arrays, you have the luxury of picking a preferred and truly personal way of doing things yourself. In some cases, one will perform better than the other with regards to what you need to achieve. I hope this comes in useful, now go do some cool stuff with some loops! Finally, feel free to copy any of the code in this article to use in your own projects or merely reference. How can we store value in array using for loop in PHP?php $a=array('a','b','c'); $c=array(); // for loop for($i=0;$i
How do you add values to an array in a while loop?Adding values of an array with each other inside a while loop. The counter stores the number of values contained within the array called numbers.. The sum should contain the sum of all the values from the array.. The first step that i completed, setup the counter variable in place (which works fine):. Can you use an array in a while loop?Using while to iterate over an array
We can even use while loops to iterate over an array. First we define an array and find out its length, len , using the length property of arrays. Then we define an index variable to iterate over the array .
Can we use for loop for array in PHP?The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
|