In our blog we have already talked about arrays in PHP, especially its creation and behavior. The idea now is to show how to perform the processing of a PHP array using a very simple scan logic.

Let’s simulate using an array of five positions where we will extract the value of each position from a counter. To do this we will use a repeating structure and other resources available in the language:


$array = array(1, 2, 3, 4, 5);
$count = count($array);

for ($i = 0; $i < $count; $i++) {

echo $array[‘$i’];


The applied logic works like this: first, it creates an array with the desired values ​​using the array () function. Then, a variable is created with the number of total positions in the array, so the variable has in this case the value 5. For the processing of each position, a repetition structure “for” is created, starting with a counter in zero. Now comes the part that requires greater attention. Every array, by default, starts its position count at 0. Thus, the control variable also needs to boot with that value. As the array has 5 positions and starts its counter in 0, you must create a logical rule in which only values ​​below the total amount should be processed. Otherwise, when accessing a non-existent position, the code will display an error. So one must always indicate that the control variable needs to be lower (<) than the total size of the array. The access to the array the position will be given by the indication of the control variable ($ i).

For our example we have a processing logic that can be applied to demands from simple to more complex. This same type of processing can also be done using the foreach command, whose principle is pretty much the same of the repeating structure used, the difference is that the command uses fewer indicators and control variables, as can be seen in the example below:

<select name=”cities”>

$cities = array(‘Curitiba’,’Sao Paulo’,’Recife’,’Rio de Janeiro’);

            foreach($cities  as $values){

echo ‘<option>’.$values.'</option>’;



In the example we have an HTML element being created in real time, creating the option selections from an array that contains the names of some cities.

Check out more content on our blog!
Learn all about Scriptcase.

By ,

June 3, 2015


You might also like…

12 IT Trends to Keep an Eye on in 2023

The year 2023 has already started in full swing bringing news and projecting trends for the web dev...

Low-code: it’s the future, not a passing trend

Low-code tools enable professionals to develop fast, efficient, and precise systems. Low-code ha...

What’s new in PHP 8.1

In this article, we will talk a little about the new features, changes, and improvements coming wit...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at Learn more about our Privacy Police.